"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 });
exports.registerWorkflowConfigTools = registerWorkflowConfigTools;
const configService = __importStar(require("../../../services/config"));
const ltconfig_1 = require("../../../modules/ltconfig");
const cron_1 = require("../../../services/cron");
const schemas_1 = require("./schemas");
function registerWorkflowConfigTools(server) {
    // mirrors GET /api/workflows/config
    server.registerTool('list_workflow_configs', {
        title: 'List Workflow Configs',
        description: 'List all certified workflow configurations. Shows workflow type, ' +
            'task queue, roles, invocable flag, and description.',
        inputSchema: schemas_1.listWorkflowConfigsSchema,
    }, async (_args) => {
        const configs = await configService.listWorkflowConfigs();
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        count: configs.length,
                        workflows: configs.map((c) => ({
                            workflow_type: c.workflow_type,
                            invocable: c.invocable,
                            task_queue: c.task_queue,
                            default_role: c.default_role,
                            description: c.description,
                            roles: c.roles,
                        })),
                    }),
                }],
        };
    });
    // mirrors PUT /api/workflows/:type/config
    server.registerTool('upsert_workflow_config', {
        title: 'Upsert Workflow Config',
        description: 'Create or replace a workflow configuration (certify). Activates the ' +
            'interceptor for task tracking, escalation chains, and invocation controls. ' +
            'Invalidates the config cache and restarts cron if schedule changes.',
        inputSchema: schemas_1.upsertWorkflowConfigSchema,
    }, async (args) => {
        const config = await configService.upsertWorkflowConfig({
            workflow_type: args.workflow_type,
            invocable: args.invocable,
            task_queue: args.task_queue,
            default_role: args.default_role,
            description: args.description,
            execute_as: args.execute_as,
            roles: args.roles,
            invocation_roles: args.invocation_roles,
            consumes: args.consumes,
            tool_tags: args.tool_tags,
            envelope_schema: args.envelope_schema,
            resolver_schema: args.resolver_schema,
            cron_schedule: args.cron_schedule,
        });
        ltconfig_1.ltConfig.invalidate();
        await cron_1.cronRegistry.restartCron(config);
        return {
            content: [{ type: 'text', text: JSON.stringify(config) }],
        };
    });
    // mirrors DELETE /api/workflows/:type/config
    server.registerTool('delete_workflow_config', {
        title: 'Delete Workflow Config',
        description: 'De-certify a workflow by removing its lt_config_workflows entry. ' +
            'The workflow remains durable but loses interceptor wrapping.',
        inputSchema: schemas_1.deleteWorkflowConfigSchema,
    }, async (args) => {
        const deleted = await configService.deleteWorkflowConfig(args.workflow_type);
        if (!deleted) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'Workflow config not found' }) }],
                isError: true,
            };
        }
        ltconfig_1.ltConfig.invalidate();
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({ deleted: true, workflow_type: args.workflow_type }),
                }],
        };
    });
}
