/**
 * 认证相关 API
 */
import { request } from './request'
import type { UserInfo } from '@/types'

/** 登录请求参数 */
export interface LoginParams {
  username: string
  password: string
  captcha: string
  captcha_key: string
}

/** 登录响应 */
export interface LoginResponse {
  access_token: string
  refresh_token: string
  token_type: string
  expires_in: number
  user: UserInfo
}

/** 刷新 Token 参数 */
export interface RefreshTokenParams {
  refresh_token: string
}

/** 刷新 Token 响应 */
export interface RefreshTokenResponse {
  access_token: string
  expires_in: number
}

/** 修改密码参数 */
export interface ChangePasswordParams {
  old_password: string
  new_password: string
  confirm_password: string
}

/**
 * 用户登录
 */
export const login = (data: LoginParams) => {
  return request.post<LoginResponse>('/auth/login', data)
}

/**
 * 刷新 Token
 */
export const refreshToken = (data: RefreshTokenParams) => {
  return request.post<RefreshTokenResponse>('/auth/refresh', data)
}

/**
 * 用户登出
 */
export const logout = () => {
  return request.post('/auth/logout')
}

/**
 * 修改密码
 */
export const changePassword = (data: ChangePasswordParams) => {
  return request.put('/auth/change-password', data)
}

/**
 * 获取验证码
 */
export const getCaptcha = () => {
  return request.get<{
    captcha_key: string
    captcha_image: string
  }>('/auth/captcha')
}
