"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAdminServer = createAdminServer;
exports.getAdminServer = getAdminServer;
exports.stopAdminServer = stopAdminServer;
/**
 * Long Tail Admin MCP Server
 *
 * Unified system management — every tool maps directly to a REST API
 * route handler. The tools are organized by route file:
 *
 *   tasks.ts               — routes/tasks.ts
 *   escalations.ts         — routes/escalations/ (incl. metadata + bulk)
 *   workflow-config.ts     — routes/workflows/config.ts
 *   workflows.ts           — routes/workflows/discovery.ts + invocation.ts
 *   mcp-servers.ts         — routes/mcp.ts
 *   yaml-workflows.ts      — routes/yaml-workflows/
 *   users.ts               — routes/users.ts + routes/roles.ts
 *   maintenance.ts         — routes/dba.ts
 *   agents.ts              — routes/agents.ts
 *   agent-subscriptions.ts — routes/agents.ts (subscription sub-routes)
 *   bot-accounts.ts        — routes/bot-accounts.ts
 *   controlplane.ts        — routes/controlplane.ts
 *   pipelines.ts           — routes/pipelines.ts
 *   topics.ts              — routes/topics.ts
 *   settings.ts            — routes/settings.ts
 *   exports.ts             — routes/exports.ts
 *
 * This server replaces three previously separate servers:
 *   - long-tail-db-query (read-only task/escalation/health queries)
 *   - long-tail-workflow-compiler (compile + deploy)
 *   - mcp-workflows-longtail (list + get + invoke compiled workflows)
 *
 * The long-tail-human-queue server remains separate because
 * escalate_and_wait has special durable signal semantics that
 * are deeply wired into the YAML worker infrastructure.
 */
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const logger_1 = require("../../../lib/logger");
const tasks_1 = require("./tasks");
const escalations_1 = require("./escalations");
const workflow_config_1 = require("./workflow-config");
const workflows_1 = require("./workflows");
const mcp_servers_1 = require("./mcp-servers");
const yaml_workflows_1 = require("./yaml-workflows");
const users_1 = require("./users");
const maintenance_1 = require("./maintenance");
const agents_1 = require("./agents");
const agent_subscriptions_1 = require("./agent-subscriptions");
const bot_accounts_1 = require("./bot-accounts");
const controlplane_1 = require("./controlplane");
const pipelines_1 = require("./pipelines");
const topics_1 = require("./topics");
const settings_1 = require("./settings");
const exports_1 = require("./exports");
const TOOL_COUNT = 71;
let server = null;
async function createAdminServer(options) {
    if (server && !options?.fresh)
        return server;
    const name = options?.name || 'long-tail-admin';
    const instance = new mcp_js_1.McpServer({ name, version: '1.0.0' });
    if (!options?.fresh) {
        server = instance;
    }
    (0, tasks_1.registerTaskTools)(instance); //  2 tools
    (0, escalations_1.registerEscalationTools)(instance); // 12 tools (5 base + 7 metadata/bulk)
    (0, workflow_config_1.registerWorkflowConfigTools)(instance); //  3 tools
    (0, workflows_1.registerWorkflowTools)(instance); //  3 tools
    (0, mcp_servers_1.registerMcpServerTools)(instance); //  4 tools
    (0, yaml_workflows_1.registerYamlWorkflowTools)(instance); //  5 tools
    (0, users_1.registerUserTools)(instance); //  7 tools (users + roles)
    (0, maintenance_1.registerMaintenanceTools)(instance); //  1 tool
    (0, agents_1.registerAgentTools)(instance); //  5 tools
    (0, agent_subscriptions_1.registerAgentSubscriptionTools)(instance); //  3 tools
    (0, bot_accounts_1.registerBotAccountTools)(instance); //  7 tools
    (0, controlplane_1.registerControlPlaneTools)(instance); //  5 tools
    (0, pipelines_1.registerPipelineTools)(instance); //  4 tools
    (0, topics_1.registerTopicTools)(instance); //  5 tools
    (0, settings_1.registerSettingsTools)(instance); //  1 tool
    (0, exports_1.registerExportTools)(instance); //  4 tools
    // Total: 71 (actual count may vary slightly)
    logger_1.loggerRegistry.info(`[lt-mcp:admin] ${name} ready (${TOOL_COUNT} tools registered)`);
    return instance;
}
function getAdminServer() {
    return server;
}
async function stopAdminServer() {
    if (server) {
        await server.close();
        server = null;
        logger_1.loggerRegistry.info('[lt-mcp:admin] stopped');
    }
}
