import { api } from './client'

export interface Provider {
  id: string
  name: string
  type: string
  base_url: string
  models: string[]
  env_key: string
  no_key_required: boolean
  configured: boolean
}

export interface ModelInUse {
  agent_id: string
  agent_name: string
  version: number
  provider: string
  model: string
}

export const providersApi = {
  list: () => api.get<{ providers: Provider[] }>('/providers'),
  save: (id: string, body: { api_key?: string; base_url?: string }) =>
    api.post<{ ok: boolean }>(`/providers/${id}`, body),
  remove: (id: string) => api.delete<{ ok: boolean }>(`/providers/${id}`),
  test: (id: string) => api.post<{ ok: boolean; message: string }>(`/providers/${id}/test`),
  /** 各智能体当前使用的模型一览 */
  modelsInUse: () => api.get<{ items: ModelInUse[]; count: number }>('/providers/models-in-use'),
  /** 切换某个智能体的模型（生成新版本，可回滚） */
  switchModel: (agentId: string, provider: string, name: string) =>
    api.put<{ agent_id: string; version: number; previous: string }>(
      `/agents/${agentId}/model`, { provider, name }),
}
