/**
 * 用户服务
 * 
 * 提供用户管理相关 API 调用
 */

import { get, post, put, del } from '@/lib/http/client'
import type { PaginatedData } from '@/types/http'
import type { Role } from '@/types/auth'
import type {
  User,
  UserStatus,
  CreateUserRequest,
  UserListParams,
  UpdateUserRequest,
  UpdateUserStatusRequest,
  AssignUserRolesRequest,
} from '@/types/user'

/**
 * 获取用户列表
 * GET /api/v1/users
 */
export function listUsers(params: UserListParams): Promise<PaginatedData<User>> {
  return get<PaginatedData<User>>('/users', params as Record<string, string | number | boolean | undefined>)
}

/**
 * 获取用户详情
 * GET /api/v1/users/:id
 */
export function getUser(id: number): Promise<User> {
  return get<User>(`/users/${id}`)
}

/**
 * 创建用户
 * POST /api/v1/users
 */
export function createUser(data: CreateUserRequest): Promise<User> {
  return post<User>('/users', data)
}

/**
 * 更新用户信息
 * PUT /api/v1/users/:id
 */
export function updateUser(id: number, data: UpdateUserRequest): Promise<User> {
  return put<User>(`/users/${id}`, data)
}

/**
 * 删除用户
 * DELETE /api/v1/users/:id
 */
export function deleteUser(id: number): Promise<void> {
  return del<void>(`/users/${id}`)
}

/**
 * 更新用户状态
 * PUT /api/v1/users/:id/status
 */
export function updateUserStatus(id: number, data: UpdateUserStatusRequest): Promise<void> {
  return put<void>(`/users/${id}/status`, data)
}

/**
 * 获取用户角色列表
 * GET /api/v1/users/:id/roles
 */
export function getUserRoles(id: number): Promise<Role[]> {
  return get<Role[]>(`/users/${id}/roles`)
}

/**
 * 分配用户角色
 * PUT /api/v1/users/:id/roles
 */
export function assignUserRoles(id: number, data: AssignUserRolesRequest): Promise<void> {
  return put<void>(`/users/${id}/roles`, data)
}

/**
 * 批量更新用户状态
 * PUT /api/v1/users/batch/status
 */
export function batchUpdateStatus(ids: number[], status: UserStatus): Promise<{ count: number }> {
  return put<{ count: number }>('/users/batch/status', { ids, status })
}

/**
 * 批量删除用户
 * DELETE /api/v1/users/batch
 */
export function batchDelete(ids: number[]): Promise<{ count: number }> {
  return del<{ count: number }>('/users/batch', { body: { ids } })
}
