import { request } from '@/api/request'

/** PC 列表查询参数 */
export interface PCListParams {
  page?: number
  size?: number
  keyword?: string
  collect_on?: boolean
}

/** 资源使用率信息 */
export interface ResourceInfo {
  value: number // 使用率百分比
  total?: string // 总量描述
  used?: string // 已用描述
}

/** PC 数据结构 */
export interface PCItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  pc_identity: string
  name: string
  host: string
  ip_address: string
  description: string
  os: string
  os_version: string
  kernel: string
  server_type: string
  tags: string
  location: string
  remote_access: string
  remote_port: number
  agent_config: string
  status: string
  last_check_time: string
  collect_on: boolean
  collect_args: string
  collect_interval: number
  collect_last_result: string
  // 资源使用率（前端展示用，可能来自采集数据）
  cpu_info?: ResourceInfo
  memory_info?: ResourceInfo
  disk_info?: ResourceInfo
}

/** PC 列表返回数据 */
export interface PCListResult {
  total: number
  page: number
  page_size: number
  data: PCItem[]
}

/** PC 创建参数 */
export interface PCCreateParams {
  pc_identity?: string
  name: string
  host: string
  ip_address?: string
  description?: string
  os?: string
  os_version?: string
  kernel?: string
  server_type?: string
  tags?: string
  location?: string
  remote_access?: string
  remote_port?: number
  agent_config?: string
  status?: string
  last_check_time?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
}

/** PC 更新参数 */
export interface PCUpdateParams {
  id: number
  pc_identity?: string
  name?: string
  host?: string
  ip_address?: string
  description?: string
  os?: string
  os_version?: string
  kernel?: string
  server_type?: string
  tags?: string
  location?: string
  remote_access?: string
  remote_port?: number
  agent_config?: string
  status?: string
  last_check_time?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
}

/** 获取PC列表（分页） */
export const fetchPCList = (params?: PCListParams) => {
  return request.get<PCListResult>('/DC-Control/v1/pcs', { params })
}

/** 获取PC详情 */
export const fetchPCDetail = (id: number) => {
  return request.get<PCItem>(`/DC-Control/v1/pcs/${id}`)
}

/** 创建PC */
export const createPC = (data: PCCreateParams) => {
  return request.post<PCItem>('/DC-Control/v1/pcs', data)
}

/** 更新PC */
export const updatePC = (id: number, data: Omit<PCUpdateParams, 'id'>) => {
  return request.put<{ message: string }>(`/DC-Control/v1/pcs/${id}`, data)
}

/** 删除PC */
export const deletePC = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/pcs/${id}`)
}
