import type { AxiosResponse } from 'axios'
import request from '@/utils/request'
import type { CacpOrganization, CacpUser, Result } from '@cacp/ui'

const contextPath = '/auth'

export async function getRootOrganization(): Promise<Result<CacpOrganization>> {
  const res: AxiosResponse<Result<CacpOrganization>> = await request.get(`${contextPath}/get-root-organization`)
  return res.data
}
export async function getOrganization(orgId: string): Promise<Result<CacpOrganization>> {
  const res: AxiosResponse<Result<CacpOrganization>> = await request.get(
    `${contextPath}/get-organization?orgId=${orgId}`
  )
  return res.data
}

export async function getOrganizationByPath(orgPath: string): Promise<Result<CacpOrganization>> {
  const res: AxiosResponse<Result<CacpOrganization>> = await request.get(
    `${contextPath}/get-organization-by-path?orgPath=${encodeURIComponent(orgPath)}`
  )
  return res.data
}

export async function getOrganizationListByParent(
  parentPath: string,
  oneLevel: boolean
): Promise<Result<Array<CacpOrganization>>> {
  const res: AxiosResponse<Result<Array<CacpOrganization>>> = await request.get(
    `${contextPath}/get-organization-list-by-parent?parentPath=${encodeURIComponent(parentPath)}&oneLevel=${oneLevel}`
  )
  return res.data
}

export async function getUser(parentId: string, userId: string): Promise<Result<CacpUser>> {
  const res: AxiosResponse<Result<CacpUser>> = await request.get(
    `${contextPath}/get-user?parentId=${parentId}&userId=${userId}`
  )
  return res.data
}

export async function getUserListById(userId: string): Promise<Result<Array<CacpUser>>> {
  const res: AxiosResponse<Result<Array<CacpUser>>> = await request.get(
    `${contextPath}/get-user-list-by-id?userId=${userId}`
  )
  return res.data
}

export async function getUserListByParent(parentPath: string, oneLevel: boolean): Promise<Result<Array<CacpUser>>> {
  const res: AxiosResponse<Result<Array<CacpUser>>> = await request.get(
    `${contextPath}/get-user-list-by-parent?parentPath=${encodeURIComponent(parentPath)}&oneLevel=${oneLevel}`
  )
  return res.data
}

export async function queryUserListByCondition(parentPath: string, keyword: string): Promise<Result<Array<CacpUser>>> {
  const res: AxiosResponse<Result<Array<CacpUser>>> = await request.get(
    `${contextPath}/query-user-list-by-condition?parentPath=${encodeURIComponent(parentPath)}&keyword=${keyword}`
  )
  return res.data
}
