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

# %% auto 0
__all__ = ['upsert_domoaccount', 'modify_generate_domo_access_token_config_fn',
           'modify_generate_destination_domo_access_token_config_fn', '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
from copy import deepcopy

import domolibrary.client.DomoAuth as dmda
import domolibrary.classes.DomoAccount as dmac

import httpx

# %% ../nbs/00_FactoryAccount.ipynb 4
@factory_function
async def upsert_domoaccount(
    config: FactoryConfig,
    res: FactoryResponse,
    
    account_name: str,
    modify_config_fn: Callable = None,

    # set during execution
    auth: dmda.DomoAuth = None,
    domo_account: dmac.DomoAccount = None,
    domo_account_config: dmac.AccountConfig = 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_name = config.account_name
        domo_account_config = config.domo_account_config
        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:
        step.message = f"trying upsert {account_name}"
        domo_account = await dmac.DomoAccounts.upsert_account(
            auth=auth,
            account_config=domo_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
async def modify_generate_domo_access_token_config_fn(
    config : FactoryConfig,
    step: FactoryMessage,
    account_config_cls: dmac.AccountConfig.domo_access_token.value,
    domo_access_token,
    user_email,
    user_password,
    **kwargs,
):

    step.message = f"updating config.domo_account_config w. {user_email}"
    config.domo_account_config = account_config_cls(
        domo_access_token=domo_access_token, username=user_email, password=user_password
    )

    step.is_success = True
    step.message = f"updated config.domo_account_config w. {user_email}"

    return config

async def modify_generate_destination_domo_access_token_config_fn(
    config : FactoryConfig,
    step: FactoryMessage,
    user_email = None,
    domo_access_token = None,
    user_password = None,
    account_name :str = None, 
    account_config_cls: dmac.AccountConfig = dmac.AccountConfig.domo_access_token.value,
    destination_auth: dmda.DomoAuth = None, 
    **kwargs
):
    if account_name:
        config.account_name = account_name
        step.message = f"set config.account_name to {account_name}"
        
    if destination_auth:
        step.message = f"set config.auth to {destination_auth.domo_instance}"
        config.auth = deepcopy(destination_auth)
    
    config.location = config.auth.domo_instance

    step.message = f"updating config.domo_account_config w. {user_email}"
    config.domo_account_config = account_config_cls(
        domo_access_token=domo_access_token, username=user_email, password=user_password
    )

    step.is_success = True
    step.message = f"updated config.domo_account_config w. {user_email}"

    return config

@dataclass
class Config_UpsertDomoAccessTokenAccount(FactoryConfig):
    account_name: str

    ## account params
    username: str
    access_token: str = None
    password: str = None
    
    modify_config_fn : Callable = modify_generate_domo_access_token_config_fn

    # updated during execution
    domo_account: dmac.DomoAccount = None
