import request from '@/utils/request'
import type { AxiosResponse } from 'axios'

interface RegisterData {
  [key: string]: any
}

export interface UserInfo {
  userId: string | number
  userName: string
  nickName: string
  avatar?: string
  roles?: string[]
  permissions?: string[]
  [key: string]: any
}

export interface GetInfoResponse {
  user: UserInfo
  roles: string[]
  permissions: string[]
  isDefaultModifyPwd?: boolean
  isPasswordExpired?: boolean
  [key: string]: any
}

interface LoginResponse {
  token?: string
  [key: string]: any
}

interface CaptchaResponse {
  captchaEnabled?: boolean
  img?: string
  uuid?: string
}

// 登录方法
export function login(username: string, password: string, code?: string, uuid?: string): Promise<AxiosResponse<LoginResponse>> {
  const data: string = `username=${username}&password=${password}&code=${code}&uuid=${uuid}`
  return request({
    url: '/api/login/account',
    headers: {
      isToken: false,
      repeatSubmit: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data: data
  })
}
export function authorize(params:string): Promise<AxiosResponse<any>> {
  return request({
    url: '/oauth2/authorize',
    headers: {
      isToken: true,
      repeatSubmit: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data: params
  })
}

// 注册方法
export function register(data: RegisterData): Promise<AxiosResponse<any>> {
  return request({
    url: '/register',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 获取用户详细信息
export function getInfo(): Promise<AxiosResponse<GetInfoResponse>> {
  return request({
    url: '/getInfo',
    method: 'get'
  })
}

// 退出方法
export function logout(): Promise<AxiosResponse<any>> {
  return request({
    url: '/logout',
    method: 'post'
  })
}

// 获取验证码
export function getCodeImg(): Promise<AxiosResponse<CaptchaResponse>> {
  return request({
    url: '/captcha/image',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}