import { API_GROUP_MEMBERS, API_GROUPS } from '@/constants/api';
import { request } from '@umijs/max';

export async function apiQueryGroups(
  params: MasterModel.GroupQueryParams,
): Promise<MasterModel.GroupResponse> {
  return request<MasterModel.GroupQueryParams>(API_GROUPS, {
    params: params,
  });
}

export async function apiQueryGroupsById(
  id: string,
): Promise<MasterModel.GroupResponse> {
  return request(`${API_GROUPS}/${id}`);
}

export async function apiQueryMembers(
  user_id: string,
): Promise<MasterModel.GroupResponse> {
  return request(`${API_GROUP_MEMBERS}/${user_id}/groups`);
}

export async function apiQueryChildren(
  group_id: string,
  params: MasterModel.GroupQueryParams,
): Promise<MasterModel.GroupResponse> {
  return request(`${API_GROUPS}/${group_id}/children`, {
    params: params,
  });
}

export async function apiUpdateGroup(body: Partial<MasterModel.GroupNode>) {
  return request<MasterModel.GroupNode>(`${API_GROUPS}/${body.id}`, {
    method: 'PUT',
    data: body,
  });
}
export async function apiCreateGroup(
  body: Partial<MasterModel.GroupBodyRequest>,
) {
  return request<MasterModel.AddGroupBodyResponse>(`${API_GROUPS}`, {
    method: 'POST',
    data: body,
    contentType: 'application/json',
  });
}

export async function apiDeleteGroup(group_id: string) {
  return request<MasterModel.AddGroupBodyResponse>(
    `${API_GROUPS}/${group_id}`,
    {
      method: 'DELETE',
      getResponse: true,
    },
  );
}

export async function apiUnassignToGroup(
  body: MasterModel.AssignMemberRequest,
) {
  return request(`${API_GROUPS}/${body.group_id}/members`, {
    method: 'DELETE',
    data: body,
    getResponse: true,
  });
}
export async function apiAssignToGroup(body: MasterModel.AssignMemberRequest) {
  return request(`${API_GROUPS}/${body.group_id}/members`, {
    method: 'POST',
    data: body,
    getResponse: true,
  });
}
