# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/routes/user.ipynb.

# %% auto 0
__all__ = ['get_all_users', 'generate_search_users_body_by_id', 'generate_search_users_body_by_email', 'process_v1_search_users',
           'SearchUser_NoResults', 'search_users', 'search_virtual_user_by_subscriber_instance', 'create_user',
           'set_user_landing_page', 'reset_password', 'request_password_reset']

# %% ../../nbs/routes/user.ipynb 3
import utils.DictDot as dd
import domolibrary.client.get_data as gd
import domolibrary.client.ResponseGetData as rgd
import domolibrary.client.DomoAuth as dmda
import domolibrary.client.DomoError as de


# %% ../../nbs/routes/user.ipynb 5
async def get_all_users(
    auth: dmda.DomoAuth, debug_api: bool = False
) -> rgd.ResponseGetData:

    """retrieves all users from Domo"""
    url = f"https://{ auth.domo_instance}.domo.com/api/content/v2/users"

    return await gd.get_data(url=url, method="GET", auth=auth, debug_api=debug_api)

# %% ../../nbs/routes/user.ipynb 9
def generate_search_users_body_by_id(
    user_ids: list[str],  # list of user ids to search
) -> dict:  # dict to pass to search v1_users_search_api
    """search v1_users_search_api"""

    return {
        # "showCount": true,
        # "count": false,
        "includeDeleted": False,
        "includeSupport": False,
        "filters": [
            {"field": "id", "filterType": "value", "values": user_ids, "operator": "EQ"}
        ],
    }

# %% ../../nbs/routes/user.ipynb 10
def generate_search_users_body_by_email(
    user_email_ls: list[ str ],  # list of user emails to search.  Note:  search does not appear to be case sensitive
) -> dict:  # dict to pass to search v1_users_search_api
    """search v1_users_search_api"""

    return {
        # "showCount": true,
        # "count": false,
        "includeDeleted": False,
        "includeSupport": False,
        "limit": 200,
        "offset": 0,
        "sort": {"field": "displayName", "order": "ASC"},
        "filters": [
            {
                "filterType": "text",
                "field": "emailAddress",
                "text": " ".join(user_email_ls),
            }
        ],
    }

# %% ../../nbs/routes/user.ipynb 11
def process_v1_search_users(v1_user_ls: list[dict] # list of users from v1_users_search API
) -> list[dict]: # sanitized list of users.
    """sanitizes the response from v1_users_search API and removes unecessary attributes"""

    clean_users = []

    for obj_user in v1_user_ls:
        
        dd_user = dd.DictDot(obj_user)

        clean_users.append( {
            "id": dd_user.id,
            "displayName": dd_user.displayName,
            "roleId": dd_user.roleId,
            "userName": dd_user.userName,
            "emailAddress": dd_user.emailAddress,
        })
    
    return clean_users


# %% ../../nbs/routes/user.ipynb 12
class SearchUser_NoResults(de.DomoError):
    def __init__(self, domo_instance,  function_name = "search_users"):
        super().__init__(message = "query returned no users", function_name = function_name)

async def search_users(
    auth: dmda.DomoAuth,
    body: dict,
    debug_api: bool = False,
    return_raw: bool = False
) -> rgd.ResponseGetData:

    url = f"https://{auth.domo_instance}.domo.com/api/identity/v1/users/search"

    res = await gd.get_data(
        url=url,
        method="POST",
        auth=auth,
        body=body,
        debug_api=debug_api,
    )

    if return_raw:
        return res

    if not res.is_success:
        return res

    if res.is_success and len(res.response.get('users')) == 0:
        raise SearchUser_NoResults(domo_instance = auth.domo_instance)
    
    res.response = process_v1_search_users(res.response.get('users'))
    return res



# %% ../../nbs/routes/user.ipynb 16
async def search_virtual_user_by_subscriber_instance(
    auth: dmda.DomoAuth, # domo auth object
    subscriber_instance_ls : list[str], # list of subscriber domo instances
    debug_api: bool = False, # debug API requests
) -> rgd.ResponseGetData: # list of virtual domo users
    """retrieve virtual users for subscriber instances tied to one publisher"""

    url = f"https://{auth.domo_instance}.domo.com/api/publish/v2/proxy_user/domain/"

    body = {"domains": [f"{subscriber_instance}.domo.com"
                        for subscriber_instance in subscriber_instance_ls]}

    return await gd.get_data(
        url=url,
        method="POST",
        auth=auth,
        body=body,
        debug_api=debug_api,
    )

# %% ../../nbs/routes/user.ipynb 20
async def create_user(
    auth: dmda.DomoAuth,
    display_name: str,
    email: str,
    role_id: int,
    debug_api: bool = False,
) -> rgd.ResponseGetData:

    url = f"https://{auth.domo_instance}.domo.com/api/content/v3/users"

    body = {"displayName": display_name, "detail": {"email": email}, "roleId": role_id}

    return await gd.get_data(
        url=url, method="POST", body=body, auth=auth, debug_api=debug_api
    )

# %% ../../nbs/routes/user.ipynb 21
async def set_user_landing_page(
    auth: dmda.DomoAuth, user_id: str, page_id: str, debug_api: bool = False
):

    url = f"https://{auth.domo_instance}.domo.com/api/content/v1/landings/target/DESKTOP/entity/PAGE/id/{page_id}/{user_id}"

    return await gd.get_data(
        url=url,
        method="PUT",
        auth=auth,
        # body = body,
        debug_api=debug_api,
    )

# %% ../../nbs/routes/user.ipynb 22
async def reset_password(
    auth: dmda.DomoAuth,
    user_id: str,
    new_password: str,
    debug_api: bool = False,
) -> rgd.ResponseGetData:

    url = f"https://{auth.domo_instance}.domo.com/api/identity/v1/password"

    body = {"domoUserId": user_id, "password": new_password}

    return await gd.get_data(
        url=url,
        method="PUT",
        auth=auth,
        body=body,
        debug_api=debug_api,
    )

# %% ../../nbs/routes/user.ipynb 23
async def request_password_reset(
    domo_instance: str, email: str, locale="en-us", debug_api: bool = False
):
    url = f"https://{domo_instance}.domo.com/api/domoweb/auth/sendReset"

    params = {"email": email, "local": locale}

    return await gd.get_data(
        url=url, method="GET", params=params, auth=None, debug_api=debug_api
    )
