import { api } from './http';

export type LLMKind = 'openai' | 'openai-compatible' | 'anthropic' | 'ollama' | 'tencent-tokenplan';

export interface LLMProvider {
  id: string;
  name: string;
  kind: LLMKind;
  baseUrl: string;
  apiKeyMasked: string;
  hasApiKey: boolean;
  models: string[];
  defaultModel: string;
  enabled: boolean;
  isDefault: boolean;
  createdAt: number;
  updatedAt: number;
}

export interface AgentAllowedLLMResponse {
  agentId: string;
  model: string;
}

export const LLMProviderAPI = {
  list: () => api.get<LLMProvider[]>('/llm-providers').then((r) => r.data),
  allowedModels: (agentId: string) => api.get<AgentAllowedLLMResponse>('/llm-providers', { params: { agent_id: agentId } }).then((r) => r.data),
  create: (payload: Partial<LLMProvider> & { apiKey?: string }) => api.post<{ id: string }>('/llm-providers', payload).then((r) => r.data),
  update: (id: string, payload: Partial<LLMProvider> & { apiKey?: string }) => api.put(`/llm-providers/${id}`, payload).then((r) => r.data),
  remove: (id: string) => api.delete(`/llm-providers/${id}`).then((r) => r.data),
  test: (id: string, model?: string) =>
    api.post<{ ok: boolean; reply?: string; error?: string; usage?: any; model?: string }>(`/llm-providers/${id}/test`, { model }).then((r) => r.data),
  setDefault: (id: string) => api.post(`/llm-providers/${id}/default`).then((r) => r.data)
};

