import { getMcpxServerURLSync } from "@/config/api-config";

// Config type for Cursor/Claude Desktop agents
export type McpServersConfig = {
  mcpServers: Record<string, unknown>;
};

// Config type for VSCode
export type VSCodeConfig = {
  servers: Record<string, unknown>;
};

// config type for warp (single server json)
export type WarpConfig = { mcpx: { serverUrl: string } };

// Config type for Codex (TOML format)
export type TomlConfig = {
  toml: string;
};

// Config type for custom MCP client agents
export type CustomMcpConfig = {
  description: string;
  streamableHttpExample: { transport: string; code: string };
  sseExample: { transport: string; code: string };
  clientSetup: { code: string };
};

export interface AgentType {
  value: string;
  label: string;
  description: string;
  getConfig: () =>
    | McpServersConfig
    | VSCodeConfig
    | CustomMcpConfig
    | TomlConfig
    | WarpConfig
    | null; // TODO: removed null after creating a new config
}

export const getAgentConfigs = (): AgentType[] => {
  return [
    {
      value: "cursor",
      label: "Cursor",
      description: "Connect Cursor to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";
        return {
          mcpServers: {
            mcpx: {
              url: mcpxUrl,
            },
          },
        };
      },
    },
    {
      value: "claudeDesktop",
      label: "Claude Desktop",
      description: "Connect Claude Desktop to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";
        return {
          mcpServers: {
            mcpx: {
              command: "npx",
              args: ["mcp-remote@0.1.36", mcpxUrl],
            },
          },
        };
      },
    },
    {
      value: "ClaudeCode",
      label: "Claude Code",
      description: "Connect Claude Code to MCPX for MCP tool integration",
      getConfig: () => {
        return null;
      },
    },
    {
      value: "warp",
      label: "Warp",
      description: "Connect Warp to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";
        return {
          mcpx: {
            serverUrl: mcpxUrl,
          },
        };
      },
    },
    {
      value: "vscode",
      label: "VSCode",
      description: "Connect VSCode to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";
        return {
          servers: {
            mcpx: {
              url: mcpxUrl,
              type: "http",
            },
          },
        };
      },
    },
    {
      value: "copilot",
      label: "Copilot",
      description: "Connect Copilot to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";
        return {
          servers: {
            mcpx: {
              url: mcpxUrl,
              type: "http",
            },
          },
        };
      },
    },
    {
      value: "openai-mcp",
      label: "ChatGPT",
      description: "Connect ChatGPT to MCPX for MCP tool integration",
      getConfig: () => {
        return null;
      },
    },
    {
      value: "codex-mcp-client",
      label: "Codex Desktop",
      description: "Connect Codex to MCPX for MCP tool integration",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync() + "/mcp";

        return {
          toml: `
[mcp_servers.mcpx]
enabled = true
url = "${mcpxUrl}"
`,
        };
      },
    },
    {
      value: "n8n",
      label: "n8n",
      description: "Connect N8N workflow to MCPX for MCP tool integration",
      getConfig: () => {
        return null;
      },
    },
    {
      value: "custom",
      label: "Custom MCP Client",
      description: "Connect your custom MCP client to MCPX",
      getConfig: () => {
        const mcpxUrl = getMcpxServerURLSync();
        return {
          description:
            "MCPX is essentially a MCP server, just like any other. Connecting to it using the SDK is similar to any MCP integration. Because MCPX adopts a remote-first approach - that is, it is meant to be deployed on the cloud - it accepts SSE connections and not stdio ones.",
          streamableHttpExample: {
            transport: "StreamableHttp",
            code: `const transport = new StreamableHTTPClientTransport(
            new URL(${mcpxUrl}/mcp),
            {
              requestInit: {
                headers: {
                  "x-lunar-consumer-tag": "my_agent_name",
                },
              },
            }
          );`,
          },
          sseExample: {
            transport: "SSE",
            code: `const transport = new SSEClientTransport(new URL(${mcpxUrl}/sse), {
              eventSourceInit: {
                fetch: (url, init) => {
                  const headers = new Headers(init?.headers);
                  const consumerTag = "my_agent_name";
                  headers.set("x-lunar-consumer-tag", consumerTag);
                  return fetch(url, { ...init, headers });
                },
              },
            });`,
          },
          clientSetup: {
            code: `const client = new Client({
              name: "mcpx-client",
              version: "1.0.0"
            });

            await client.connect(transport);`,
          },
        };
      },
    },
  ];
};
