"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const api = __importStar(require("../api/mcp"));
const router = (0, express_1.Router)();
// ── Server registration CRUD ──────────────────────────────────────────
/**
 * GET /api/mcp/servers
 * List registered MCP servers.
 */
router.get('/servers', async (req, res) => {
    const tagsParam = req.query.tags;
    const result = await api.listMcpServers({
        status: req.query.status,
        auto_connect: req.query.auto_connect === 'true' ? true :
            req.query.auto_connect === 'false' ? false : undefined,
        search: req.query.search,
        tags: tagsParam ? tagsParam.split(',').map((t) => t.trim()).filter(Boolean) : undefined,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/mcp/servers
 * Register a new MCP server.
 */
router.post('/servers', async (req, res) => {
    const { name, description, transport_type, transport_config, auto_connect, metadata, tags, compile_hints, credential_providers } = req.body;
    const result = await api.createMcpServer({
        name,
        description,
        transport_type,
        transport_config,
        auto_connect,
        metadata,
        tags,
        compile_hints,
        credential_providers,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/mcp/servers/test-connection
 * Test connectivity to an MCP server without persisting it.
 */
router.post('/servers/test-connection', async (req, res) => {
    const { transport_type, transport_config } = req.body;
    const result = await api.testConnection({ transport_type, transport_config });
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Parameterized routes (must come after literal paths) ──────────────
/**
 * GET /api/mcp/servers/:id
 * Get a specific MCP server.
 */
router.get('/servers/:id', async (req, res) => {
    const result = await api.getMcpServer({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/mcp/servers/:id
 * Update an MCP server registration.
 */
router.put('/servers/:id', async (req, res) => {
    const result = await api.updateMcpServer({ id: req.params.id, ...req.body });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/mcp/servers/:id
 * Delete an MCP server registration.
 */
router.delete('/servers/:id', async (req, res) => {
    const result = await api.deleteMcpServer({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Connection management ─────────────────────────────────────────────
/**
 * POST /api/mcp/servers/:id/connect
 * Connect to a registered MCP server.
 */
router.post('/servers/:id/connect', async (req, res) => {
    const result = await api.connectMcpServer({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/mcp/servers/:id/disconnect
 * Disconnect from a specific MCP server.
 */
router.post('/servers/:id/disconnect', async (req, res) => {
    const result = await api.disconnectMcpServer({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Credential status ────────────────────────────────────────────────
/**
 * GET /api/mcp/servers/:id/credential-status
 * Check which credential providers are required and which the user has.
 */
router.get('/servers/:id/credential-status', async (req, res) => {
    const result = await api.getCredentialStatus({ id: req.params.id }, req.auth ? { userId: req.auth.userId } : undefined);
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Tool operations ───────────────────────────────────────────────────
/**
 * GET /api/mcp/servers/:id/tools
 * List tools available on a connected MCP server.
 */
router.get('/servers/:id/tools', async (req, res) => {
    const result = await api.listMcpServerTools({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/mcp/servers/:id/tools/:toolName/call
 * Call a tool on a connected MCP server.
 * Body: { arguments: { ... } }
 */
router.post('/servers/:id/tools/:toolName/call', async (req, res) => {
    const result = await api.callMcpTool({
        id: req.params.id,
        toolName: req.params.toolName,
        arguments: req.body.arguments,
        execute_as: req.body.execute_as,
    }, req.auth ? { userId: req.auth.userId } : undefined);
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
