import typer
from typing_extensions import Annotated
from typing import Optional
from typing import Tuple
import os
import json
import plugin_ix as px
from enum import Enum
import pandas as pd
from rich.table import Table
from rich import print
import uuid
import cms
import requests


FAILED = '[ [red] \u2717 [/red] ]'
PASSED = '[ [green] \u2713 [/green] ]'
cli = typer.Typer()
@cli.command(name="set")
def set_auth (
        id:Annotated[str,typer.Argument(help="identifier used in setting cookies")],
        manifest:Annotated[str,typer.Argument(help="path manifest or site folder")],
        registry:Annotated[str,typer.Argument(help="path to the registry created by plugin-ix")],
        authpath:Annotated[str,typer.Argument(help="Authentication file location, that contains the authentication model")]
         ):
    """
    This function will set login configuration to a manifest
    """
    try:
        _args   = {"id":id,"registry":registry,"path":authpath,"authorization":{}}
        path    = cms.engine.config.get_manifest(manifest=manifest)
        _config = cms.engine.config.get(path)
        _config['system']['source'] = {'secure':_args}
        _secEngine = cms.secure.Manager(config=_config)
        cms.engine.config.write(config=_config,path=path)
    except Exception as e:
        print (f"""{FAILED} [bold][red]{id}, failed [/red][/bold], error found {str(e)}""")
    #
    # we should try to test this configuration to see if it works
    #

    pass
def permissions():
    pass
def inspect ():
    pass
@cli.command("drop")
def remove (manifest:Annotated[str,typer.Argument(help="path manifest or site folder")]):
    """
    This function removes login configuration from a manifest
    """
    path = cms.engine.config.get_manifest(manifest=manifest)
    _config = cms.engine.config.get(path)
    _msg = f'{FAILED}'
    try:
        if 'secure' in _config['system']['source'] :
            del _config['system']['source']['secure']
            cms.engine.config.write(config=_config,path=path)
            _msg = f'{PASSED} [green]Successfully[/green] removed secure attribute'
    except Exception as e:
        _msg = f'{_msg}, error found {str(e)}'
    
    print (_msg)
        
    pass
@cli.command(name="template")
def gen_auth (
        _model:Annotated[str,typer.Argument(help="generate an authentication parameter file")],
):
    """
    This function generates a template login file content for a model (pam, oauth2, nextcloud)
    """
    _conf = {"method":_model}
    _map = {"pam":{"age":3600},"nextcloud":{"url":"https://your-nextcloud-site"}}
    #
    # parameters client_id, client-secret, 
    _map["oauth2"] = {"client_id":"client_id","client_secret":"client_secret","scope":"profile","response_type":"code","authorization_url":"authorization_url","redirect_uri":"redirect_uri"}
    if _model in _map :
        _conf = dict(_conf, **_map.get(_model))
        print (_conf)
        print ()
        print(f'{PASSED} [bold] Edit[/bold] and [bold]save[/bold] the content into an [bold]"authentication file"[/bold]')
    else:
        print (f"""{FAILED} [bold]{_model}[/bold] is not a supported authentication model""")