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

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

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

# %% ../nbs/00_FactoryAccount.ipynb 2
from dataclasses import dataclass
from typing import Callable
import datetime as dt

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

import httpx

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

    
    account_name : str,
    account_config_cls : str,
    generate_account_config_fn : Callable,

    modify_config_fn: Callable = None,

    # set during execution
    auth : dmda.DomoAuth = None,
    domo_account: dmac.DomoAccount = None ,


    # used by wrapper
    session : httpx.AsyncClient = None,
    debug_api: bool = False,
    **kwargs

):
    res.response = False
    domo_account = config.domo_account or None

    if modify_config_fn:
        step = FactoryMessage(stage="executing modify_config_fn", stage_num=1)
        step.is_success = False
        res.add_message(step)

        
        # print(config,step, debug_api)

        await modify_config_fn(
           **config.asdict(), config=config, step=step, debug_api=debug_api
        )

        domo_account = config.domo_account
        account_config_cls = config.account_config_cls
        account_name = config.account_name
        auth = config.auth

    res.location = auth.domo_instance

    step = FactoryMessage( stage="upsert domo account", is_success=False, stage_num = 2)
    res.add_message( step )
     
    try:
        account_config = generate_account_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 
           
    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_account_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


