"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDbServer = createDbServer;
exports.getDbServer = getDbServer;
exports.stopDbServer = stopDbServer;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const logger_1 = require("../../../lib/logger");
const tools_1 = require("./tools");
let server = null;
/**
 * Create the Long Tail DB Query MCP server.
 *
 * Provides read-only query tools against the lt_* tables:
 * - find_tasks — search tasks by status, workflow type, or origin
 * - find_escalations — search escalations by status, role, type
 * - get_process_summary — aggregate process view grouped by origin_id
 * - get_escalation_stats — real-time escalation statistics
 * - get_workflow_types — list registered workflow configurations
 * - get_system_health — overall system health snapshot
 */
async function createDbServer(options) {
    if (server && !options?.fresh)
        return server;
    const name = options?.name || 'long-tail-db-query';
    const instance = new mcp_js_1.McpServer({ name, version: '1.0.0' });
    // Only cache as the singleton when not a fresh (dedicated) instance
    if (!options?.fresh) {
        server = instance;
    }
    (0, tools_1.registerTools)(instance);
    logger_1.loggerRegistry.info(`[lt-mcp:db-server] ${name} ready (6 tools registered)`);
    return instance;
}
/**
 * Get the current DB MCP server instance.
 */
function getDbServer() {
    return server;
}
/**
 * Stop the DB MCP server and release resources.
 */
async function stopDbServer() {
    if (server) {
        await server.close();
        server = null;
        logger_1.loggerRegistry.info('[lt-mcp:db-server] stopped');
    }
}
