import { get, post, put } from '../request'

export interface ComputeNode {
  id: string
  code: string
  name: string
  api_base_url: string
  file_gateway_url: string
  enabled: boolean
  scheduler_status: string
  scheduler_weight: number
  tags: string[]
  gpu_count: number
  current_running_jobs: number
  max_parallel_jobs: number
  data_root: string
  model_root: string
  log_root: string
  last_health_check_at?: string
}

export interface ComputeGpu {
  id: number
  node_id: string
  node_code: string
  node_name: string
  name: string
  uuid: string
  status: string
  gpu_percent: number
  memory_used_gb: number
  memory_total_gb: number
  memory_percent: number
  temperature: number
  power_w: number
  power_limit_w: number
  processes?: Array<{
    pid: number
    name: string
    memory_used_gb: number
    task_name?: string
    user?: string
  }>
}

export interface ComputeQueueItem {
  id: string
  name: string
  status: string
  progress: number
  compute_node_id?: string
  gpus: number[]
  create_time?: string
}

export interface ResourceReplica {
  id: string
  node_id: string
  resource_type: string
  resource_id: string
  local_path: string
  status: string
  sync_status: string
  create_time: string
}

export const getComputeNodes = () => get<ComputeNode[]>('/compute/nodes')

export const updateComputeNode = (id: string, data: Partial<ComputeNode>) =>
  put<ComputeNode>(`/compute/nodes/${id}`, data)

export const testComputeNode = (id: string) =>
  post<{ node_id: string; success: boolean; latency_ms: number }>(`/compute/nodes/${id}/test-connection`)

export const enableComputeNode = (id: string) => post<ComputeNode>(`/compute/nodes/${id}/enable`)

export const disableComputeNode = (id: string) => post<ComputeNode>(`/compute/nodes/${id}/disable`)

export const drainComputeNode = (id: string) => post<ComputeNode>(`/compute/nodes/${id}/drain`)

export const getComputeGpus = () => get<ComputeGpu[]>('/compute/gpus')

export const getComputeQueue = () => get<ComputeQueueItem[]>('/compute/queue')

export const getNodeReplicas = (id: string) => get<ResourceReplica[]>(`/compute/nodes/${id}/replicas`)
