/**
 * 用户管理 API
 */
import { request } from './request'
import type {
  UserInfo,
  PaginationParams,
  PaginationResponse
} from '@/types'

/** 用户列表参数 */
export interface UserListParams extends PaginationParams {
  keyword?: string
  status?: string
  role_id?: number
}

/** 创建用户参数 */
export interface UserCreateParams {
  username: string
  password: string
  realName: string
  email?: string
  phone?: string
  roleIds: number[]
}

/** 更新用户参数 */
export type UserUpdateParams = Partial<Pick<UserCreateParams, 'realName' | 'email' | 'phone'>> & {
  status?: string
}

/**
 * 获取用户列表
 */
export const getUserList = (params: UserListParams) => {
  return request.get<PaginationResponse<UserInfo>>('/users', { params })
}

/**
 * 获取用户详情
 */
export const getUserById = (id: number) => {
  return request.get<UserInfo>(`/users/${id}`)
}

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

/**
 * 创建用户
 */
export const createUser = (data: UserCreateParams) => {
  return request.post<UserInfo>('/users', data)
}

/**
 * 更新用户
 */
export const updateUser = (id: number, data: UserUpdateParams) => {
  return request.put(`/users/${id}`, data)
}

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

/**
 * 重置用户密码
 */
export const resetUserPassword = (id: number, newPassword: string) => {
  return request.post(`/users/${id}/reset-password`, { new_password: newPassword })
}
