import request from '@/utils/request'
import { UserStatus } from '@/constants/status'

export interface User {
  id: number
  username: string
  nickname: string
  email: string
  phone: string
  avatar: string
  status: UserStatus
  roles: number[]
  createdAt: string
  updatedAt: string
}

export interface CreateUserRequest {
  username: string
  password: string
  nickname: string
  email: string
  phone: string
  roles: string[]
}

export interface UpdateUserRequest {
  nickname?: string
  email?: string
  phone?: string
  avatar?: string
  status?: UserStatus
  roles?: string[]
}

export interface UserPageRequest {
  page: number
  size: number
  username?: string
  nickname?: string
  status?: string
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
}

export interface PageResponse<T> {
  content: T[]
  totalElements: number
  totalPages: number
  size: number
  number: number
  first: boolean
  last: boolean
}

export const userApi = {
  getAll: () => 
    request.get<User[]>('/users'),
  
  getPage: (params: UserPageRequest) => 
    request.get<PageResponse<User>>('/users/page', { params }),
  
  getById: (id: number) => 
    request.get<User>(`/users/${id}`),
  
  create: (data: CreateUserRequest) => 
    request.post<User>('/users', data),
  
  update: (id: number, data: UpdateUserRequest) => 
    request.put<User>(`/users/${id}`, data),
  
  delete: (id: number) => 
    request.delete<void>(`/users/${id}`),
  
  batchDelete: (ids: number[]) => 
    request.post<void>('/users/batch-delete', { ids }),
  
  resetPassword: (id: number) => 
    request.post<void>(`/users/${id}/reset-password`),
  
  updateStatus: (id: number, status: UserStatus) => 
    request.put<void>(`/users/${id}/status`, { status }),
  
  assignRoles: (id: number, roleIds: number[]) => 
    request.post<void>(`/users/${id}/roles`, { roleIds }),
}
