import api from '../../../shared/utils/api';

// ============================================================
// Types
// ============================================================

export type McpTransportType = 'stdio' | 'sse';
export type McpConnectionState = 'disconnected' | 'connecting' | 'connected' | 'error';

export interface McpToolDefinition {
  name: string;
  originalName: string;
  serverId: string;
  description: string;
  input_schema: Record<string, unknown>;
  isConcurrencySafe: boolean;
}

export interface McpServerDto {
  id: string;
  serverId: string;
  name: string;
  description: string | null;
  transport: McpTransportType;
  enabled: boolean;
  connectionState: McpConnectionState;
  toolCount: number;
  lastError?: string;
  createdAt: string;
  updatedAt: string;
}

export interface McpServerDetailDto extends McpServerDto {
  command: string | null;
  args: string[] | null;
  env: Record<string, string> | null;
  url: string | null;
  toolsConcurrencySafe: boolean;
  connectTimeoutMs: number;
  toolTimeoutMs: number;
  tools: McpToolDefinition[];
  connectedAt?: string;
}

export interface McpOverviewDto {
  totalServers: number;
  connectedCount: number;
  disconnectedCount: number;
  errorCount: number;
  totalTools: number;
}

export interface CreateMcpServerDto {
  serverId: string;
  name: string;
  description?: string;
  transport: McpTransportType;
  command?: string;
  args?: string[];
  env?: Record<string, string>;
  url?: string;
  toolsConcurrencySafe?: boolean;
  connectTimeoutMs?: number;
  toolTimeoutMs?: number;
  enabled?: boolean;
}

export interface UpdateMcpServerDto {
  name?: string;
  description?: string;
  transport?: McpTransportType;
  command?: string;
  args?: string[];
  env?: Record<string, string>;
  url?: string;
  toolsConcurrencySafe?: boolean;
  connectTimeoutMs?: number;
  toolTimeoutMs?: number;
  enabled?: boolean;
}

export interface TestConnectionResult {
  success: boolean;
  error?: string;
  toolCount?: number;
}

// ============================================================
// API
// ============================================================

export const mcpApi = {
  getOverview: async (): Promise<McpOverviewDto> => {
    const response = await api.get('/conversations/admin/mcp/overview');
    return response.data.data;
  },

  listServers: async (): Promise<McpServerDto[]> => {
    const response = await api.get('/conversations/admin/mcp/servers');
    return response.data.data;
  },

  getServer: async (id: string): Promise<McpServerDetailDto> => {
    const response = await api.get(`/conversations/admin/mcp/servers/${id}`);
    return response.data.data;
  },

  createServer: async (dto: CreateMcpServerDto): Promise<McpServerDto> => {
    const response = await api.post('/conversations/admin/mcp/servers', dto);
    return response.data.data;
  },

  updateServer: async (id: string, dto: UpdateMcpServerDto): Promise<McpServerDto> => {
    const response = await api.put(`/conversations/admin/mcp/servers/${id}`, dto);
    return response.data.data;
  },

  deleteServer: async (id: string): Promise<void> => {
    await api.delete(`/conversations/admin/mcp/servers/${id}`);
  },

  connectServer: async (id: string): Promise<{ connectionState: McpConnectionState; toolCount: number; lastError?: string }> => {
    const response = await api.post(`/conversations/admin/mcp/servers/${id}/connect`);
    return response.data.data;
  },

  disconnectServer: async (id: string): Promise<void> => {
    await api.post(`/conversations/admin/mcp/servers/${id}/disconnect`);
  },

  getServerTools: async (id: string): Promise<McpToolDefinition[]> => {
    const response = await api.get(`/conversations/admin/mcp/servers/${id}/tools`);
    return response.data.data;
  },

  testServer: async (id: string): Promise<TestConnectionResult> => {
    const response = await api.post(`/conversations/admin/mcp/servers/${id}/test`);
    return response.data.data;
  },

  testConfig: async (dto: CreateMcpServerDto): Promise<TestConnectionResult> => {
    const response = await api.post('/conversations/admin/mcp/test-config', dto);
    return response.data.data;
  },
};
