import { post } from './request'

export type MpRoleType = 'mp-sales' | 'mp-dispatch' | 'mp-construction'

export interface LoginParams {
  account: string
  password: string
  roleType?: MpRoleType | 'mp'
}

export interface LoginMenu {
  menuId: number
  menuName: string
  parentId?: number
  orderNum?: number
  path?: string
  component?: string
  icon?: string
  menuType?: string
  children?: LoginMenu[]
}

export interface LoginResult {
  token: string
  userId: number
  userName: string
  nickName: string
  roleType: string
  roleId?: number
  roleName?: string
  roleKey?: string
  homePath?: string
  avatar: string
  phonenumber?: string
  phone?: string
  email?: string
  dept?: { deptName?: string; deptId?: number; name?: string }
  deptId?: number
  deptName?: string
  staffCode?: string
  employeeNo?: string
  entryDate?: string
  hireDate?: string
  teamId?: number
  team?: string
  teamName?: string
  dutyStatus?: string
  skillLevel?: string
  certifications?: string
  tags?: string
  skillTags?: string
  currentTaskId?: number
  postNames?: string
  needChangePassword?: boolean
  menus?: LoginMenu[]
  permissions?: string[]
  mpEndpoint?: 'mp-sales' | 'mp-dispatch' | 'mp-construction'
}

export function login(params: LoginParams) {
  // 不默认传 roleType，由后端根据账号配置的 mpEndpoint 判断登录端
  return post<LoginResult>('/auth/login', params)
}

export function logout() {
  return post('/auth/logout')
}

export function debugPassword() {
  return post('/auth/debug-password')
}

export function resetPassword() {
  return post('/auth/reset-password')
}

export function initData() {
  return post('/auth/init-data')
}
