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

# %% auto 0
__all__ = ['GroupType_Enum', 'generate_body_create_group', 'SearchGroups_Error', 'search_groups_by_name', 'CreateGroup_Error',
           'create_group', 'get_all_groups', 'generate_body_update_group_membership', 'update_group_membership',
           'get_group_by_id']

# %% ../../nbs/routes/group.ipynb 4
import httpx
from enum import Enum

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/group.ipynb 5
class GroupType_Enum(Enum):
        OPEN = 'open'
        ADHOC = 'adHoc'
        CLOSED = 'closed'
        DIRECTORY = 'directory'
        DYNAMIC = 'dynamic'
        SYSYTEM = 'system'

def generate_body_create_group(group_name: str,
                               group_type: str = 'open',
                               description: str = '') -> dict:
    """ Generates the body to create group for content_v2_group API"""
    body = {"name": group_name, 
            "type": group_type,
            "description": description}

    return body

# %% ../../nbs/routes/group.ipynb 8
class SearchGroups_Error(de.DomoError):
    def __init__(self, status, message, domo_instance, function_name = "search_groups_by_name"):
        super().__init__(function_name = function_name, status = status, message = message , domo_instance = domo_instance)
        
async def search_groups_by_name(auth: dmda.DomoAuth,
                                search_name: str,
                                is_exact_match: bool = True,
                                debug_api: bool = False,
                                session: httpx.AsyncClient = None
                                ) -> rgd.ResponseGetData:
    """uses /content/v2/groups/grouplist api -- includes user details"""

    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/groups/grouplist?ascending=true&search={search_name}&sort=name '

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='GET',
        debug_api=debug_api,
        session = session
    )
    if not is_exact_match:
        return res
    
    match_group = next((group for group in res.response if group.get('name') == search_name), None)
    #print(match_group)
    
    if not match_group:
        raise SearchGroups_Error(
            status=res.status,
            message=f'There is no exact match for {search_name}',
            domo_instance=auth.domo_instance
        )
    res.response = match_group
    
    return res


# %% ../../nbs/routes/group.ipynb 11
class CreateGroup_Error(de.DomoError):
    def __init__(self, status, message, domo_instance, function_name = "create_group"):
        super().__init__(function_name = function_name, status = status, message = message , domo_instance = domo_instance)

async def create_group(auth: dmda.DomoAuth,
                       group_name: str,
                       group_type: str = 'open',
                       description: str = '',
                       debug_api: bool = False,
                       session: httpx.AsyncClient = None
                       ) -> rgd.ResponseGetData:
    # body : {"name": "GROUP_NAME", "type": "open", "description": ""}

    body = generate_body_create_group(
        group_name=group_name, group_type=group_type, description=description)

    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/groups/'

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

    if not res.is_success:
        group_exists = await search_groups_by_name(auth=auth, search_name=group_name, is_exact_match=True)
        if group_exists.is_success:
            raise CreateGroup_Error(
                status = res.status,
                message = f'{group_name} already exists. Choose a different group_name',
                domo_instance = auth.domo_instance,
                function_name='create_group'
            )

    if not res.is_success:
        raise CreateGroup_Error(
            status = res.status, 
            message = res.response,
            domo_instance = auth.domo_instance, 
            function_name="create_group")

    return res


# %% ../../nbs/routes/group.ipynb 14
async def get_all_groups(auth: dmda.DomoAuth,
                         debug_api: bool = False,
                         session: httpx.AsyncClient = None) -> rgd.ResponseGetData:
    """uses /content/v2/groups/grouplist api -- includes user details"""

    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/groups/grouplist'

    res = await gd.get_data(url=url,
                            method='GET',
                            auth=auth, 
                            debug_api=debug_api, session=session)

    return res


# %% ../../nbs/routes/group.ipynb 17
def generate_body_update_group_membership(group_id: str,
                                          add_user_arr: list[str] = None,
                                          remove_user_arr: list[str] = None,
                                          add_owner_user_arr: list[str] = None,
                                          remove_owner_user_arr: list[str] = None) -> list[dict]:
    body = {"groupId": int(group_id)}
    if add_owner_user_arr:
        body.update({"addOwners": [{"type": "USER", "id": str(
            userId)} for userId in add_owner_user_arr]})

    if remove_owner_user_arr:
        body.update({"removeOwners": [{"type": "USER", "id": str(
            userId)} for userId in remove_owner_user_arr]})

    if remove_user_arr:
        body.update({"removeMembers": [
                    {"type": "USER", "id": str(userId)} for userId in remove_user_arr]})
    if add_user_arr:
        body.update(
            {"addMembers": [{"type": "USER", "id": str(userId)} for userId in add_user_arr]})

    return [body]

# %% ../../nbs/routes/group.ipynb 18
async def update_group_membership(auth: dmda.DomoAuth,
                                  body: dict, # first need to create body with generate_body_update_group_membership() 
                                  debug_api: bool = False, session: httpx.AsyncClient = None
                                  ) -> rgd.ResponseGetData:
    # body = [{
    #     "groupId":"GROUP_ID",
    #     "removeMembers": [{"type":"USER","id":"USER_ID"}],
    #     "addMembers"   : [{"type":"USER","id":"USER_ID"}]
    # }]

    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/groups/access'

    if debug_api:
        print(url, body)

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='PUT',
        body=body,
        debug_api=debug_api,
        session = session
    )

    return res

# %% ../../nbs/routes/group.ipynb 21
async def get_group_by_id(auth: dmda.DomoAuth, 
        group_id:str, 
        debug_api:bool = False,
        session : httpx.AsyncClient = None,
        ) -> rgd.ResponseGetData:

    """uses /content/v2/groups/ api -- does not return details"""
    
    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/groups/{group_id}'

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='GET',
        debug_api=debug_api,
        session = session
    )

    if res.status ==404 and res.response == 'Not Found':
        raise SearchGroups_Error(
            status = res.status,
            message = f"group {group_id} not found",
            domo_instance = auth.domo_instance,
            function_name = 'get_group_by_id'
        )

    return res
