import { api } from './client'

export interface PlatformStatus {
  agents_active: number
  runs_total: number
  runs_completed: number
  runs_failed: number
  runs_running: number
  success_rate: string
  avg_latency_ms: number
  total_tokens: number
}

export interface AgentStatus {
  agent_id: string
  name: string
  health: { score: number; level: string }
  runs_total: number
  success_rate: string
  avg_latency_ms: number
  total_tokens: number
  last_run: string | null
}

export interface RecentRun {
  run_id: string
  agent_id: string
  agent_name: string
  status: string
  input_preview: string
  workspace_path: string
  created_at: string | null
  completed_at: string | null
}

export const statusApi = {
  platform: () => api.get<PlatformStatus>('/status'),
  agents: () => api.get<{ agents: AgentStatus[]; count: number }>('/status/agents'),
  /** 跨智能体最近运行（工作台「继续上次的工作」） */
  recentRuns: (limit = 5) => api.get<{ items: RecentRun[] }>(`/status/recent-runs?limit=${limit}`),
  /** AI 助手入口：把自然语言请求路由到最合适的智能体（方案 B） */
  assistantRoute: (input: string) =>
    api.post<{ matched: boolean; agent_id?: string; agent_name?: string; reason?: string }>(
      '/assistant/route', { input }),
}
