import { del, get, post, put } from '../request'
import type {
  CreateUserPayload,
  HealthMetrics,
  LoginResponse,
  SystemInfo,
  SystemUser,
  UpdateUserAccessPayload,
} from '@/types'

/** 系统信息（CPU/内存/磁盘/GPU/网络/系统） */
export const getSystemInfo = () => get<SystemInfo>('/system-info')

/** 健康指标（顶部栏轻量指标） */
export const getHealth = () => get<HealthMetrics>('/health')

/** 登录 */
export const login = (username: string, password: string) =>
  post<LoginResponse>('/login', { username, password })

/** 用户列表 */
export const getUsers = () => get<SystemUser[]>('/users')

/** 创建用户 */
export const createUser = (payload: CreateUserPayload) =>
  post<SystemUser>('/users', payload)

/** 删除用户，currentUsername 用于防止删除当前登录账号 */
export const deleteUser = (id: string, currentUsername: string) =>
  del<{ deleted: string }>(`/users/${encodeURIComponent(id)}`, {
    current_username: currentUsername,
  })

/** 更新用户角色、状态及页面权限 */
export const updateUserAccess = (id: string, payload: UpdateUserAccessPayload) =>
  put<SystemUser>(`/users/${encodeURIComponent(id)}`, payload)
