export interface AgentToolDetail {
  name: string
  description: string
  parameters: {
    type?: string
    properties?: Record<string, { type?: string; default?: unknown; items?: unknown }>
    required?: string[]
  }
}

export interface AgentDetail {
  name: string
  display_name: string
  subtitle: string
  system_prompt: string
  document_injection: AgentDocumentInjection
  tools: AgentToolDetail[]
}

export interface AgentDocumentInjection {
  content: string
  enabled: boolean
  updated_at: string | null
}

export async function fetchAgentDetail(agentName: string): Promise<AgentDetail> {
  const response = await fetch(`/api/agents/${encodeURIComponent(agentName)}`)
  if (!response.ok) {
    throw new Error(`加载 Agent 详情失败: ${response.status} ${response.statusText}`)
  }
  return response.json()
}

export async function updateAgentDocumentInjection(
  agentName: string,
  injection: Pick<AgentDocumentInjection, 'content' | 'enabled'>,
): Promise<AgentDocumentInjection> {
  const response = await fetch(
    `/api/agents/${encodeURIComponent(agentName)}/document-injection`,
    {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(injection),
    },
  )
  if (!response.ok) {
    throw new Error(`保存文档注入失败: ${response.status} ${response.statusText}`)
  }
  return response.json()
}
