import { request } from '.'
import { mockUserAPI } from './mockData'
import { createMockWrapper } from '@/config/mock'

// 定义用户相关类型
export interface User {
  id: number
  username: string
  nickname: string
  email: string
  phone: string
  avatar: string
  gender: number
  birthday: string
  points: number
  level: number
  balance: number
  travel_count: number
  animal_adopt_count: number
  flower_order_count: number
  status: string
  remark: string
  created_at: string
  updated_at: string
  last_login_at: string
}

export interface UserQueryParams {
  page?: number
  pageSize?: number
  keyword?: string
  userType?: string
  status?: string
}

export interface UserCreateData {
  username: string
  password: string
  nickname?: string
  email?: string
  phone?: string
  gender?: string
  birthday?: string
  status?: string
  remark?: string
}

export interface UserUpdateData {
  username?: string
  password?: string
  nickname?: string
  email?: string
  phone?: string
  gender?: string
  birthday?: string
  status?: string
  remark?: string
}

export interface ApiResponse<T = any> {
  success: boolean
  code: number
  message: string
  data: T
}

export interface UserListResponse {
  users: User[]
  pagination: {
    page: number
    pageSize: number
    total: number
    totalPages: number
  }
}

// 获取用户列表
export const getUsers = (params?: UserQueryParams) =>
  request.get<ApiResponse<UserListResponse>>('/users', { params })

// 获取用户详情
export const getUser = (id: number) =>
  request.get<ApiResponse<User>>(`/users/${id}`)

// 创建用户
export const createUser = (data: UserCreateData) =>
  request.post<ApiResponse<User>>('/users', data)

// 更新用户
export const updateUser = (id: number, data: UserUpdateData) =>
  request.put<ApiResponse<User>>(`/users/${id}`, data)

// 删除用户
export const deleteUser = (id: number) =>
  request.delete<ApiResponse<void>>(`/users/${id}`)

// 批量更新用户状态
export const batchUpdateUserStatus = (userIds: number[], status: string) =>
  request.post<ApiResponse<{ message: string; affectedRows: number }>>('/users/batch-status', { userIds, status })

// 更新用户状态
export const updateUserStatus = (id: number, status: string) =>
  request.put<ApiResponse<User>>(`/users/${id}/status`, { status })

// 启用用户
export const enableUser = (id: number) =>
  request.put<ApiResponse<User>>(`/users/${id}/enable`)

// 禁用用户
export const disableUser = (id: number) =>
  request.put<ApiResponse<User>>(`/users/${id}/disable`)

// 封禁用户
export const banUser = (id: number) =>
  request.put<ApiResponse<User>>(`/users/${id}/ban`)

// 解封用户
export const unbanUser = (id: number) =>
  request.put<ApiResponse<User>>(`/users/${id}/unban`)

// 使用 mock 包装器
const userAPI = createMockWrapper({
  getUsers,
  getUser,
  createUser,
  updateUser,
  deleteUser,
  batchUpdateUserStatus,
  enableUser,
  disableUser,
  banUser,
  unbanUser
}, mockUserAPI)

export default userAPI