import api from './index'

export interface AgentStats {
  agent_id: string
  call_count: number
  current_task: string | null
  status: 'active' | 'idle' | 'disabled'
}

export interface AgentHierarchyStatsNode extends AgentStats {
  sub_commanders: AgentStats[]
}

export interface AgentHierarchyStats {
  main_agents: AgentHierarchyStatsNode[]
}

export interface AgentConfig {
  id: string
  name: string
  role: string
  description: string
  system_prompt: string
  enabled: boolean
  selected_skill_ids?: string[]
}

export interface AgentVisibilityEvent {
  event_id: string
  event_type: string
  timestamp: string
  conversation_id?: string | null
  agent_id?: string | null
  task_id?: string | null
  thread_id?: string | null
  severity: string
  payload: Record<string, unknown>
}

export interface AgentVisibilityVerifier {
  conversation_id: string
  status?: string | null
  summary?: string | null
  evidence: Array<Record<string, unknown>>
}

export interface AgentVisibilityTaskSummary {
  task_id: string
  role?: string | null
  owner_agent_id?: string | null
  status?: string | null
  summary?: string | null
  evidence_count: number
}

export interface AgentVisibilityTopologyNode {
  agent_id: string
  role?: string | null
  parent_agent_id?: string | null
  source: string
  task_count: number
  completed_task_count: number
}

export interface AgentVisibilityTopology {
  conversation_id: string
  root_agent_id?: string | null
  current_agent?: string | null
  nodes: AgentVisibilityTopologyNode[]
  edges: Array<Record<string, string>>
  tasks: AgentVisibilityTaskSummary[]
  task_hierarchy: Record<string, string[]>
}

export interface AgentVisibilityIsolation {
  mode: string
  isolation_id?: string | null
  workspace_path?: string | null
  parent_conversation_id?: string | null
  metadata: Record<string, unknown>
}

export interface AgentVisibilityCost {
  input_tokens: number
  output_tokens: number
  total_tokens: number
  estimated_cost?: number | null
  budget_warning: boolean
  currency: string
}

export interface AgentVisibilityCostByAgent {
  agent_id: string
  input_tokens: number
  output_tokens: number
  total_tokens: number
  estimated_cost?: number | null
  budget_warning: boolean
}

export interface AgentVisibilityCostSummary {
  conversation_id: string
  total: AgentVisibilityCost
  thresholds: Record<string, number>
  by_agent: AgentVisibilityCostByAgent[]
}

export interface AgentVisibilityToolGovernanceItem {
  capability_id: string
  tool_name: string
  permission_class: string
  side_effect_scope: string
  supports_retry: boolean
  idempotent: boolean
  safe_for_parallel_use: boolean
  requires_confirmation: boolean
  usage_count: number
  last_result_preview?: string | null
}

export interface AgentVisibilityToolGovernance {
  conversation_id: string
  total_tools: number
  used_tools: number
  items: AgentVisibilityToolGovernanceItem[]
  upgrade_candidates: string[]
}

export interface AgentVisibilityRuntimeSummary {
  conversation_id: string
  execution_mode?: string | null
  current_phase?: string | null
  current_checkpoint?: string | null
  phase_history: Array<Record<string, unknown>>
  checkpoint_history: Array<Record<string, unknown>>
  verifier: AgentVisibilityVerifier
  isolation: AgentVisibilityIsolation
  cost: AgentVisibilityCost
  topology_node_count: number
  active_task_count: number
  completed_task_count: number
  recent_events: AgentVisibilityEvent[]
}

export const agentApi = {
  async getStats(): Promise<AgentStats[]> {
    const res = await api.get('/api/agents/stats')
    return res.data
  },

  async getHierarchyStats(): Promise<AgentHierarchyStats> {
    const res = await api.get('/api/agents/stats/hierarchy')
    return res.data
  },

  async getConfig(id: string): Promise<AgentConfig> {
    const res = await api.get(`/api/agents/config/${id}`)
    return res.data
  },

  async updateConfig(id: string, data: Partial<AgentConfig>): Promise<AgentConfig> {
    const res = await api.put(`/api/agents/config/${id}`, data)
    return res.data
  },

  async getRuntimeSummary(conversationId: string): Promise<AgentVisibilityRuntimeSummary> {
    const res = await api.get('/api/agents/visibility/runtime-summary', {
      params: { conversation_id: conversationId },
    })
    return res.data
  },

  async getVisibilityTopology(conversationId: string): Promise<AgentVisibilityTopology> {
    const res = await api.get('/api/agents/visibility/topology', {
      params: { conversation_id: conversationId },
    })
    return res.data
  },

  async getVisibilityVerifier(conversationId: string): Promise<AgentVisibilityVerifier> {
    const res = await api.get('/api/agents/visibility/verifier', {
      params: { conversation_id: conversationId },
    })
    return res.data
  },

  async getVisibilityCost(conversationId: string): Promise<AgentVisibilityCostSummary> {
    const res = await api.get('/api/agents/visibility/cost', {
      params: { conversation_id: conversationId },
    })
    return res.data
  },

  async getVisibilityTools(conversationId: string): Promise<AgentVisibilityToolGovernance> {
    const res = await api.get('/api/agents/visibility/tools', {
      params: { conversation_id: conversationId },
    })
    return res.data
  },
}
