"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.registerTaskTools = registerTaskTools;
const taskService = __importStar(require("../../../services/task"));
const escalationService = __importStar(require("../../../services/escalation"));
const schemas_1 = require("./schemas");
function registerTaskTools(server) {
    server.registerTool('find_tasks', {
        title: 'Find Tasks',
        description: 'Search tasks with optional filters. Returns task records with ' +
            'workflow_id, status, workflow_type, and timestamps.',
        inputSchema: schemas_1.findTasksSchema,
    }, async (args) => {
        const { tasks, total } = await taskService.listTasks({
            status: args.status,
            workflow_type: args.workflow_type,
            workflow_id: args.workflow_id,
            origin_id: args.origin_id,
            limit: args.limit,
            offset: args.offset,
        });
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        total,
                        count: tasks.length,
                        tasks: tasks.map((t) => ({
                            id: t.id,
                            workflow_id: t.workflow_id,
                            workflow_type: t.workflow_type,
                            status: t.status,
                            priority: t.priority,
                            origin_id: t.origin_id,
                            created_at: t.created_at,
                            completed_at: t.completed_at,
                            error: t.error,
                        })),
                    }),
                }],
        };
    });
    server.registerTool('get_process_detail', {
        title: 'Get Process Detail',
        description: 'Get all tasks and escalations for a process (origin_id). ' +
            'Returns the full history of a multi-step workflow execution.',
        inputSchema: schemas_1.getProcessDetailSchema,
    }, async (args) => {
        const [tasks, escalations] = await Promise.all([
            taskService.getProcessTasks(args.origin_id),
            escalationService.getEscalationsByOriginId(args.origin_id),
        ]);
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        origin_id: args.origin_id,
                        task_count: tasks.length,
                        escalation_count: escalations.length,
                        tasks: tasks.map((t) => ({
                            id: t.id,
                            workflow_id: t.workflow_id,
                            workflow_type: t.workflow_type,
                            status: t.status,
                            created_at: t.created_at,
                            completed_at: t.completed_at,
                        })),
                        escalations: escalations.map((e) => ({
                            id: e.id,
                            type: e.type,
                            role: e.role,
                            status: e.status,
                            created_at: e.created_at,
                        })),
                    }),
                }],
        };
    });
}
