import { http } from './http'
import type {
  ApiRecord,
  ApiTokenRecord,
  Authority,
  CaptchaInfo,
  Dictionary,
  DictionaryDetail,
  LoginLog,
  LoginResult,
  MenuNode,
  OperationRecord,
  PagePayload,
  SysErrorRecord,
  SysParam,
  UserInfo,
} from '@/types/system'

export const authApi = {
  getCaptcha() {
    return http.post<CaptchaInfo>('/base/captcha')
  },
  login(payload: { username: string; password: string; captcha?: string; captchaId?: string }) {
    return http.post<LoginResult>('/base/login', payload)
  },
  getUserInfo() {
    return http.get<{ userInfo: UserInfo }>('/user/getUserInfo')
  },
  logout() {
    return http.post<Record<string, never>>('/jwt/jsonInBlacklist')
  },
  changeAuthority(authorityId: number) {
    return http.post<Record<string, never>>('/user/setUserAuthority', { authorityId })
  },
  setSelfInfo(payload: Partial<UserInfo>) {
    return http.put<Record<string, never>>('/user/setSelfInfo', payload)
  },
  changePassword(payload: { password: string; newPassword: string }) {
    return http.post<Record<string, never>>('/user/changePassword', payload)
  },
}

export const menuApi = {
  getMenu() {
    return http.post<{ menus: MenuNode[] }>('/menu/getMenu')
  },
  getBaseMenuTree() {
    return http.post<{ menus: MenuNode[] }>('/menu/getBaseMenuTree')
  },
  getMenuList() {
    return http.post<MenuNode[]>('/menu/getMenuList')
  },
  addBaseMenu(payload: Partial<MenuNode>) {
    return http.post<Record<string, never>>('/menu/addBaseMenu', payload)
  },
  updateBaseMenu(payload: Partial<MenuNode>) {
    return http.post<Record<string, never>>('/menu/updateBaseMenu', payload)
  },
  deleteBaseMenu(id: number) {
    return http.post<Record<string, never>>('/menu/deleteBaseMenu', { ID: id })
  },
  getBaseMenuById(id: number) {
    return http.post<{ menu: MenuNode }>('/menu/getBaseMenuById', { ID: id })
  },
  getMenuAuthority(authorityId: number) {
    return http.post<{ menus: MenuNode[] }>('/menu/getMenuAuthority', { authorityId })
  },
  addMenuAuthority(payload: { authorityId: number; menus: MenuNode[] }) {
    return http.post<Record<string, never>>('/menu/addMenuAuthority', payload)
  },
  getMenuRoles(menuId: number) {
    return http.get<{ authorityIds: number[]; defaultRouterAuthorityIds: number[] }>('/menu/getMenuRoles', {
      params: { menuId },
    })
  },
  setMenuRoles(payload: { menuId: number; authorityIds: number[] }) {
    return http.post<Record<string, never>>('/menu/setMenuRoles', payload)
  },
}

export const authorityApi = {
  getAuthorityList() {
    return http.post<Authority[]>('/authority/getAuthorityList', {})
  },
  createAuthority(payload: Partial<Authority>) {
    return http.post<Record<string, never>>('/authority/createAuthority', payload)
  },
  updateAuthority(payload: Partial<Authority>) {
    return http.put<{ authority: Authority }>('/authority/updateAuthority', payload)
  },
  deleteAuthority(authorityId: number) {
    return http.post<Record<string, never>>('/authority/deleteAuthority', { authorityId })
  },
  copyAuthority(payload: { oldAuthorityId: number; authority: Partial<Authority> }) {
    return http.post<Record<string, never>>('/authority/copyAuthority', payload)
  },
  getUsersByAuthorityId(authorityId: number) {
    return http.get<number[]>('/authority/getUsersByAuthority', { params: { authorityId } })
  },
  setRoleUsers(payload: { authorityId: number; userIds: number[] }) {
    return http.post<Record<string, never>>('/authority/setRoleUsers', payload)
  },
}

export const userApi = {
  getUserList(payload: Record<string, unknown>) {
    return http.post<PagePayload<UserInfo>>('/user/getUserList', payload)
  },
  register(payload: Record<string, unknown>) {
    return http.post<Record<string, never>>('/user/admin_register', payload)
  },
  updateUser(payload: Record<string, unknown>) {
    return http.put<Record<string, never>>('/user/setUserInfo', payload)
  },
  deleteUser(id: number) {
    return http.delete<Record<string, never>>('/user/deleteUser', { data: { ID: id } })
  },
  resetPassword(payload: { ID: number; password: string }) {
    return http.post<Record<string, never>>('/user/resetPassword', payload)
  },
}

export const casbinApi = {
  getPolicyPathByAuthorityId(authorityId: number) {
    return http.post<{ paths: Array<{ path: string; method: string }> }>('/casbin/getPolicyPathByAuthorityId', {
      authorityId,
    })
  },
  updateCasbin(payload: { authorityId: number; casbinInfos: Array<{ path: string; method: string }> }) {
    return http.post<Record<string, never>>('/casbin/updateCasbin', payload)
  },
}

export const systemApi = {
  getSystemConfig() {
    return http.post<{ config: Record<string, unknown> }>('/system/getSystemConfig')
  },
  setSystemConfig(payload: Record<string, unknown>) {
    return http.post<Record<string, never>>('/system/setSystemConfig', payload)
  },
  reloadSystem() {
    return http.post<Record<string, never>>('/system/reloadSystem')
  },
}

export const apiRegistryApi = {
  getApiList(payload: Record<string, unknown>) {
    return http.post<PagePayload<ApiRecord>>('/api/getApiList', payload)
  },
  getAllApis() {
    return http.post<{ apis: ApiRecord[] }>('/api/getAllApis', {})
  },
  createApi(payload: Partial<ApiRecord>) {
    return http.post<Record<string, never>>('/api/createApi', payload)
  },
  updateApi(payload: Partial<ApiRecord>) {
    return http.post<Record<string, never>>('/api/updateApi', payload)
  },
  deleteApi(payload: { ID: number }) {
    return http.post<Record<string, never>>('/api/deleteApi', payload)
  },
  getApiById(id: number) {
    return http.post<{ api: ApiRecord }>('/api/getApiById', { ID: id })
  },
  freshCasbin() {
    return http.get<Record<string, never>>('/api/freshCasbin')
  },
  getApiRoles(path: string, method: string) {
    return http.get<number[]>('/api/getApiRoles', { params: { path, method } })
  },
  setApiRoles(payload: { path: string; method: string; authorityIds: number[] }) {
    return http.post<Record<string, never>>('/api/setApiRoles', payload)
  },
}

export const dictionaryApi = {
  getDictionaryList(params?: Record<string, unknown>) {
    return http.get<Dictionary[]>('/sysDictionary/getSysDictionaryList', { params })
  },
  createDictionary(payload: Partial<Dictionary>) {
    return http.post<Record<string, never>>('/sysDictionary/createSysDictionary', payload)
  },
  updateDictionary(payload: Partial<Dictionary>) {
    return http.put<Record<string, never>>('/sysDictionary/updateSysDictionary', payload)
  },
  deleteDictionary(payload: { ID: number }) {
    return http.delete<Record<string, never>>('/sysDictionary/deleteSysDictionary', { data: payload })
  },
  getDictionaryTree(sysDictionaryID: number) {
    return http.get<{ list: DictionaryDetail[] }>('/sysDictionaryDetail/getDictionaryTreeList', {
      params: { sysDictionaryID },
    })
  },
  createDictionaryDetail(payload: Partial<DictionaryDetail>) {
    return http.post<Record<string, never>>('/sysDictionaryDetail/createSysDictionaryDetail', payload)
  },
  updateDictionaryDetail(payload: Partial<DictionaryDetail>) {
    return http.put<Record<string, never>>('/sysDictionaryDetail/updateSysDictionaryDetail', payload)
  },
  deleteDictionaryDetail(payload: { ID: number }) {
    return http.delete<Record<string, never>>('/sysDictionaryDetail/deleteSysDictionaryDetail', { data: payload })
  },
}

export const sysParamsApi = {
  getParamsList(params?: Record<string, unknown>) {
    return http.get<PagePayload<SysParam>>('/sysParams/getSysParamsList', { params })
  },
  createParam(payload: Partial<SysParam>) {
    return http.post<Record<string, never>>('/sysParams/createSysParams', payload)
  },
  updateParam(payload: Partial<SysParam>) {
    return http.put<Record<string, never>>('/sysParams/updateSysParams', payload)
  },
  deleteParam(id: number) {
    return http.delete<Record<string, never>>('/sysParams/deleteSysParams', { params: { ID: id } })
  },
}

export const loginLogApi = {
  getLoginLogList(params?: Record<string, unknown>) {
    return http.get<PagePayload<LoginLog>>('/sysLoginLog/getLoginLogList', { params })
  },
  deleteLoginLog(id: number) {
    return http.delete<Record<string, never>>('/sysLoginLog/deleteLoginLog', { data: { ID: id } })
  },
  deleteLoginLogByIds(ids: number[]) {
    return http.delete<Record<string, never>>('/sysLoginLog/deleteLoginLogByIds', { data: { ids } })
  },
}

export const operationApi = {
  getOperationList(params?: Record<string, unknown>) {
    return http.get<PagePayload<OperationRecord>>('/sysOperationRecord/getSysOperationRecordList', { params })
  },
  deleteOperation(id: number) {
    return http.delete<Record<string, never>>('/sysOperationRecord/deleteSysOperationRecord', { data: { ID: id } })
  },
  deleteOperationByIds(ids: number[]) {
    return http.delete<Record<string, never>>('/sysOperationRecord/deleteSysOperationRecordByIds', { data: { ids } })
  },
}

export const apiTokenApi = {
  getTokenList(payload: Record<string, unknown>) {
    return http.post<PagePayload<ApiTokenRecord>>('/sysApiToken/getApiTokenList', payload)
  },
  createToken(payload: { userId: number; authorityId: number; days: number; remark?: string }) {
    return http.post<{ token: string }>('/sysApiToken/createApiToken', payload)
  },
  deleteToken(id: number) {
    return http.post<Record<string, never>>('/sysApiToken/deleteApiToken', { ID: id })
  },
}

export const sysErrorApi = {
  getErrorList(params?: Record<string, unknown>) {
    return http.get<PagePayload<SysErrorRecord>>('/sysError/getSysErrorList', { params })
  },
  updateError(payload: Partial<SysErrorRecord>) {
    return http.put<Record<string, never>>('/sysError/updateSysError', payload)
  },
  deleteError(id: number) {
    return http.delete<Record<string, never>>('/sysError/deleteSysError', { params: { ID: id } })
  },
  handleError(id: number) {
    return http.get<Record<string, never>>('/sysError/getSysErrorSolution', { params: { id } })
  },
}

export const inventoryApi = {
  getServerInfo() {
    return http.post<{ server: Record<string, unknown> }>('/system/getServerInfo')
  },
}
