import { get, post, put, del } from '@/utils/request'
import type { IResponse, IBaseResponse, BaseResponse } from '@/types'

// 用户类型
export interface User {
  id: string
  username: string
  role: 'admin' | 'operator' | 'viewer'
  createdAt?: string
  updatedAt?: string
}

// 用户列表查询参数 (matches backend schema)
export interface UserQueryParams {
  page?: number
  pageSize?: number
  search?: string
  role?: 'admin' | 'operator' | 'viewer'
  status?: 'active' | 'disabled'
}

// 创建/更新用户参数
export interface UserForm {
  username: string
  password?: string
  role: 'admin' | 'operator' | 'viewer'
}

// 获取用户列表
export function listUsers(params?: UserQueryParams): Promise<IResponse<User>> {
  return get('/users', params)
}

// 获取用户详情
export function getUser(id: string): Promise<IBaseResponse<User>> {
  return get(`/users/${id}`)
}

// 创建用户
export function createUser(data: UserForm): Promise<IBaseResponse<User>> {
  return post('/users', data)
}

// 更新用户
export function updateUser(id: string, data: Partial<UserForm>): Promise<IBaseResponse<User>> {
  return put(`/users/${id}`, data)
}

// 删除用户
export function deleteUser(id: string): Promise<BaseResponse> {
  return del(`/users/${id}`)
}

// 重置用户密码
export function resetPassword(id: string, password: string): Promise<BaseResponse> {
  return post(`/users/${id}/reset-password`, { password })
}
