import { api } from './http';

export interface McpServer {
  id: string;
  name: string;
  transport: 'stdio' | 'sse' | 'http';
  command: string;
  args: string[];
  env: Record<string, string>;
  url: string;
  enabled: boolean;
  createdAt: number;
}

export interface McpStatus {
  id: string;
  name: string;
  error?: string;
  toolCount: number;
  resourceCount: number;
  promptCount: number;
  capabilities: { tools: boolean; resources: boolean; prompts: boolean };
  tools: { name: string; description?: string }[];
  resources: { uri: string; name?: string; description?: string; mimeType?: string }[];
  prompts: { name: string; description?: string; arguments?: any[] }[];
}

export const McpAPI = {
  list: (agentId: string) => api.get<McpServer[]>(`/agents/${agentId}/mcp-servers`).then((r) => r.data),
  status: (agentId: string) => api.get<McpStatus[]>(`/agents/${agentId}/mcp-status`).then((r) => r.data),
  create: (agentId: string, payload: Partial<McpServer>) => api.post(`/agents/${agentId}/mcp-servers`, payload).then((r) => r.data),
  update: (agentId: string, serverId: string, payload: Partial<McpServer>) =>
    api.put(`/agents/${agentId}/mcp-servers/${serverId}`, payload).then((r) => r.data),
  remove: (agentId: string, serverId: string) => api.delete(`/agents/${agentId}/mcp-servers/${serverId}`).then((r) => r.data),
  restart: (agentId: string) => api.post(`/agents/${agentId}/mcp-restart`).then((r) => r.data),
  importJSON: (agentId: string, config: any, replace = false) => api.post(`/agents/${agentId}/mcp-import`, { config, replace }).then((r) => r.data),
  readResource: (agentId: string, serverId: string, uri: string) => api.post(`/agents/${agentId}/mcp-read-resource`, { serverId, uri }).then((r) => r.data),
  getPrompt: (agentId: string, serverId: string, name: string, args: Record<string, any> = {}) =>
    api.post(`/agents/${agentId}/mcp-get-prompt`, { serverId, name, args }).then((r) => r.data)
};

