# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_FactoryAuth.ipynb.

# %% auto 0
__all__ = ['generate_config_auth', 'Config_GenerateConfigAuth', 'upsert_domoaccount', 'generate_config_access_token',
           'Config_UpsertDomoAccessTokenAccount']

# %% ../nbs/00_FactoryAuth.ipynb 1
from .utils.factory import (
    factory_function,
    FactoryLogs, 
    FactoryResponse, FactoryConfig, FactoryMessage)

# %% ../nbs/00_FactoryAuth.ipynb 2
from dataclasses import dataclass
from typing import Callable

import domolibrary.client.DomoAuth as dmda
import domolibrary.classes.DomoAccount as dmac
import domolibrary_execution.utils.domojupyter as dxdj

import httpx

# %% ../nbs/00_FactoryAuth.ipynb 5
@factory_function
async def generate_config_auth(
    config : FactoryConfig,
    res: FactoryResponse,
    
    config_auth_standard : dmda.DomoAuth,
    config_auth_exception : dmda.DomoAuth,
    target_instance_use_exception_pw : bool,
    target_instance : str,

    # ouptut
    auth : dmda.DomoAuth =None, # will be updated during execution
    
    **kwargs
):

    res.location = target_instance
    step = FactoryMessage(stage="generate_config_auth", message="using standard auth")
    res.add_message(step)
    auth = config_auth_standard
    step.is_success = True

    if target_instance_use_exception_pw == 1:
        auth = config_auth_exception
        step.message = "using exception auth"
        step.is_success = True

    auth.domo_instance = target_instance

    step = FactoryMessage(stage="test is valid auth")
    res.add_message(step)

    try:
        await auth.print_is_token()
        step.is_success = True
        step.message = "valid auth"

    except Exception as e:
        step.message = e
        step.is_success = False

    config.auth = auth
    res.response = auth
    return res

@dataclass
class Config_GenerateConfigAuth(FactoryConfig):
    config_auth_exception : dmda.DomoAuth
    config_auth_standard : dmda.DomoAuth
    target_instance : str
    target_instance_use_exception_pw : bool = True

    # updated during execution
    auth: dmda.DomoAuth = None

# %% ../nbs/00_FactoryAuth.ipynb 8
@factory_function
async def upsert_domoaccount(
    config: FactoryConfig,
    res : FactoryResponse,

    auth : dmda.DomoAuth,
    
    account_name : str,
    account_config_cls : str,
    generate_config_fn : Callable,

    modify_config_fn: Callable = None,
    session : httpx.AsyncClient = None,
    debug_api: bool = False,

    # used by wrapper
    **kwargs

):
    res.location = auth.domo_instance

    step = FactoryMessage( stage="upsert domo account", is_success=False)
    res.add_message( step )
    
    
    domo_account = None
    try:
        account_config = generate_config_fn(config, account_config_cls)
    
        domo_account = await dmac.DomoAccounts.upsert_account(
            auth = auth,
            account_config = account_config,
            account_name = account_name,
            session = session,
            debug_api = debug_api
        )

        config.domo_account = domo_account
    
        step.is_success = True
        step.message = f'account {account_name} upserted'
        
    except Exception as e:
        step.message = e
        step.is_success = False
        

    if modify_config_fn:
        step = FactoryMessage( stage="modify config", is_success=False)
        res.add_message( step )
    
        modify_config_fn(config = config, step = step, 
                         domo_account = domo_account)
        
           
    res.response = domo_account or 'no account'
        
    return res



# for different accounts  config object will need different fields
def generate_config_access_token(config, account_config_cls : dmac.AccountConfig.domo_access_token):
    return account_config_cls(
        domo_access_token = config.access_token,
        username = config.username,
        password = config.password
    )

@dataclass
class Config_UpsertDomoAccessTokenAccount(FactoryConfig):
    account_name : str 
    generate_config_fn : Callable
    account_config_cls : dmac.AccountConfig.domo_access_token.value

    ## account params
    username : str
    access_token : str = None
    password : str = None

    # updated during execution
    domo_account : dmac.DomoAccount = None


