import { fetch, post, ApiResponse } from './index'

interface UserInfo {
  id: string
  username: string
  fullName: string
  loginRoleId: string
  loginRoleName: string
  userRoles: Array<{ id: string; name: string }>
  attachDataList: Array<{ url: string }>
  [key: string]: any
}

interface PageResult<T> {
  records: T[]
  total: number
  current: number
  size: number
}

export function fetchUserInfo(): Promise<ApiResponse<UserInfo>> {
  return fetch({ url: '/upms/current-user/info' })
}

export function leftMenus(params = {}, headers = {}): Promise<ApiResponse<any[]>> {
  return fetch({ url: '/upms/current-user/left-menus', params, headers })
}

export function passwordCheckStatus(params = {}, headers = {}): Promise<ApiResponse<string>> {
  return fetch({ url: '/upms/current-user/check-password-status', params, headers })
}

export function switchRole(params = {}, headers = {}): Promise<ApiResponse<UserInfo>> {
  return post({ url: '/upms/current-user/switch-role', params, headers })
}

export function updateUserPwd(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/current-user/update-pwd', params, headers })
}

export function logout(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/current-user/logout', params, headers })
}

export function refreshToken(params = {}): Promise<ApiResponse<{ token: string; urlPrefix: string; ttl: number }>> {
  return post({ url: '/upms/current-user/refresh-token', params })
}

export function updateSelfUserDetail(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/current-user/update-self', params, headers })
}

export function queryUserPage(params = {}, headers = {}): Promise<ApiResponse<PageResult<any>>> {
  return fetch({ url: '/upms/admin/user/page', params, headers })
}

export function createUser(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/create', params, headers })
}

export function removeUser(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/remove', params, headers })
}

export function queryUserAccount(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return fetch({ url: '/upms/admin/user/account', params, headers })
}

export function addRole(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/role-add', params, headers })
}

export function delRole(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/role-delete', params, headers })
}

export function changeLock(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/change-lock', params, headers })
}

export function resetPwd(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/reset-pwd', params, headers })
}

export function fetchUserById(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return fetch({ url: '/upms/admin/user/detail/by-id', params, headers })
}

export function updateUserDetail(params = {}, headers = {}): Promise<ApiResponse<any>> {
  return post({ url: '/upms/admin/user/detail/update', params, headers })
}

export function uploadUserAvatar() {
  return {
    url: '/recruit-common/attachment/upload-image',
    extraData: { fileSpace: 'user-avatar', isCompress: '1' },
    uploadType: 'localOss'
  }
}
