import apiClient from './client'

// 类型定义
export interface RegisterRequest {
  username: string
  password: string
  nickName?: string
  email?: string
  phone?: string
}

export interface LoginRequest {
  username: string
  password: string
}

export interface UpdateProfileRequest {
  nickName?: string
  email?: string
  phone?: string
  avatar?: string
  preferences?: string
  aiSettings?: string
}

export interface ChangePasswordRequest {
  oldPassword: string
  newPassword: string
}

export interface User {
  id: number
  uuid: string
  username: string
  nickName: string
  email: string
  phone: string
  avatar: string
  status: string
  enable: boolean
  isAdmin: boolean
  lastLoginAt: string | null
  lastLoginIp: string
  chatCount: number
  messageCount: number
  createdAt: string
}

export interface LoginResponse {
  user: User
  token: string
  refreshToken: string
  expiresAt: number
}

// API 方法
export const authApi = {
  // 用户注册
  register: (data: RegisterRequest) => {
    return apiClient.post('/app/auth/register', data)
  },

  // 用户登录
  login: (data: LoginRequest): Promise<{ data: LoginResponse }> => {
    return apiClient.post('/app/auth/login', data)
  },

  // 刷新 Token
  refreshToken: (refreshToken: string): Promise<{ data: LoginResponse }> => {
    return apiClient.post('/app/auth/refresh', { refreshToken })
  },

  // 用户登出
  logout: () => {
    return apiClient.post('/app/auth/logout')
  },

  // 获取用户信息
  getUserInfo: (): Promise<{ data: User }> => {
    return apiClient.get('/app/auth/userinfo')
  },

  // 更新用户资料
  updateProfile: (data: UpdateProfileRequest) => {
    return apiClient.put('/app/user/profile', data)
  },

  // 修改密码
  changePassword: (data: ChangePasswordRequest) => {
    return apiClient.post('/app/user/change-password', data)
  },
}
