/**
 * 认证相关API
 */
import { request } from '../request'
import type { ApiResponse } from '../config'

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

// 登录响应数据
export interface LoginResult {
  user: {
    id: number
    username: string
    email: string
    full_name: string
    role: string
    is_active: boolean
    is_verified: boolean
    created_at?: string
    updated_at?: string
  }
  token: {
    access_token: string
    refresh_token: string
    token_type: string
  }
}

// 注册请求参数
export interface RegisterParams {
  username: string
  email: string
  password: string
  confirm_password: string
  full_name: string
}

// 用户信息
export interface UserInfo {
  id: number
  username: string
  email: string
  full_name: string
  role: string
  is_active: boolean
  created_at: string
  updated_at: string
  phone?: string
  bio?: string
  gender?: string
  avatar_url?: string
  school?: string
  major?: string
  position_name?: string // 职位名称
  teams?: any[]
  is_verified?: boolean
  last_login_at?: string
}

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

/**
 * 用户注册
 */
export const register = (data: RegisterParams): Promise<ApiResponse<UserInfo>> => {
  return request.post('/api/v1/auth/register', data)
}

/**
 * 用户登出
 */
export const logout = (): Promise<ApiResponse<null>> => {
  return request.post('/api/v1/auth/logout')
}

/**
 * 刷新Token
 */
export const refreshToken = (token: string): Promise<ApiResponse<LoginResult>> => {
  return request.post('/api/v1/auth/refresh', { refresh_token: token })
}

/**
 * 获取当前用户信息
 */
export const getCurrentUser = (): Promise<ApiResponse<UserInfo>> => {
  return request.get('/api/v1/users/me')
}

/**
 * 重置密码请求
 */
export const resetPasswordRequest = (email: string): Promise<ApiResponse<null>> => {
  return request.post('/api/v1/auth/reset-password', { email })
}
