import axios from './instance'
import {
  GET_ROLE_LIST,
  ADD_ROLE,
  DEL_ROLE,
  UPDATE_ROLE,
  ROLE_OPTIONS,
  USER_ROLE_OPTIONS,
  SELECT_OPTIONS,
  PROJECT_ROLE_OPTIONS
} from './constant'
import type { PageResult, PageRequest, Project } from './'
import { RoutesName } from '@/router'

export interface Role {
  roleId: number
  roleName: string
  remark: string
  defaultRole?: boolean
  projectName: string
  menuIds: RoleOption['id'][]
  createTime: string
}

export interface RoleOption {
  id: number
  label: string
  path: RoutesName
  children: RoleOption[]
}

export type RoleOptions = RoleOption[]
export type Roles = Role[]

export const fetchRoles = (
  params: PageRequest<
    Partial<Pick<Role, 'roleName'>> & { projectId: Project['projectId'] }
  >
) => axios.post<PageResult<Role>>(GET_ROLE_LIST, params)

export const deleteRole = (
  roleId: Role['roleId'],
  projectId: Project['projectId']
) => axios.post(DEL_ROLE, { roleId, projectId })

export const addRole = (
  projectId: Project['projectId'],
  role: Omit<Role, 'roleId' | 'createTime'>
) => axios.post(ADD_ROLE, { projectId, ...role })

export const updateRole = (role: Role) => axios.post(UPDATE_ROLE, role)

export const fetchRoleOptions = () => axios.post<RoleOptions>(ROLE_OPTIONS, {})
export const fetchUserRoleOptions = () =>
  axios.post<RoleOptions>(USER_ROLE_OPTIONS, {})

export const fetchProjectRoleOptions = (projectId: Project['projectId']) =>
  axios.post<RoleOptions>(PROJECT_ROLE_OPTIONS, { projectId })

export const fetchRoleSelects = (projectId: Project['projectId']) =>
  axios.post<Roles>(SELECT_OPTIONS, { projectId })
