import request from '@/utils/http'

export interface AgentNode {
  id: string
  code: string
  name: string
  description: string
  endpoint: string
  node_type: 'worker' | 'gateway' | 'executor'
  pool_type: 'public' | 'private'
  pool_code: string
  enabled: boolean
  status: 'pending' | 'online' | 'offline' | 'disabled'
  token_prefix: string
  version: string
  capabilities: Record<string, unknown>
  metadata: Record<string, unknown>
  last_heartbeat_at?: string
  last_heartbeat_ip?: string
  last_error: string
  created_at: string
  updated_at: string
}

export type AgentNodeInput = Pick<AgentNode, 'name' | 'description' | 'endpoint' | 'node_type' | 'pool_type' | 'pool_code' | 'enabled'> & { code?: string }

export interface AgentNodeTokenResponse { node: AgentNode; token: string; warning: string }

export interface AgentNodeRoutePreviewInput {
  pool_type: AgentNode['pool_type']
  pool_code: string
  required_capabilities: string[]
  request_key: string
}

export interface AgentNodeRoutePreview {
  pool_type: AgentNode['pool_type']
  pool_code: string
  required_capabilities: string[]
  request_key: string
  selection_policy: string
  reason: 'selected_online_node' | 'no_online_node' | 'no_capable_node'
  selected: AgentNode | null
  candidates: AgentNode[]
}

export function fetchAgentNodes() {
  return request.get<AgentNode[]>({ url: '/api/v1/admin/agent-nodes' })
}

export function createAgentNode(data: AgentNodeInput) {
  return request.post<AgentNodeTokenResponse>({ url: '/api/v1/admin/agent-nodes', params: data })
}

export function updateAgentNode(id: string, data: AgentNodeInput) {
  return request.put<AgentNode>({ url: `/api/v1/admin/agent-nodes/${id}`, params: data })
}

export function deleteAgentNode(id: string) {
  return request.del<{ deleted: boolean }>({ url: `/api/v1/admin/agent-nodes/${id}` })
}

export function rotateAgentNodeToken(id: string) {
  return request.post<AgentNodeTokenResponse>({ url: `/api/v1/admin/agent-nodes/${id}/rotate-token` })
}

export function previewAgentNodeRoute(data: AgentNodeRoutePreviewInput) {
  return request.post<AgentNodeRoutePreview>({ url: '/api/v1/admin/agent-nodes/route-preview', params: data })
}

// --- 节点任务下发 ---
export interface AgentTask {
  id: string; task_type: string; payload: Record<string, unknown>
  pool_type: string; pool_code: string; node_id?: string; node_code: string
  status: string; attempts: number; max_attempts: number
  result?: Record<string, unknown>; error: string
  created_at: string; claimed_at?: string; finished_at?: string; available_at: string
}
export const fetchAgentTasks=(status='')=>request.get<AgentTask[]>({url:'/api/v1/admin/agent-tasks',params:{status}})
export const createAgentTask=(params:{task_type:string;payload:Record<string,unknown>;pool_type?:string;pool_code?:string;node_id?:string;max_attempts?:number})=>request.post<AgentTask>({url:'/api/v1/admin/agent-tasks',params})
export const cancelAgentTask=(id:string)=>request.post<{cancelled:boolean}>({url:`/api/v1/admin/agent-tasks/${id}/cancel`})
export const retryAgentTask=(id:string)=>request.post<{retried:boolean}>({url:`/api/v1/admin/agent-tasks/${id}/retry`})
