"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.registerEscalationTools = registerEscalationTools;
const escalationService = __importStar(require("../../../services/escalation"));
const schemas_1 = require("./schemas");
function registerEscalationTools(server) {
    // mirrors GET /api/escalations
    server.registerTool('find_escalations', {
        title: 'Find Escalations',
        description: 'Search escalations with optional filters. Returns records with ' +
            'type, role, priority, status, description, and assignment info.',
        inputSchema: schemas_1.findEscalationsSchema,
    }, async (args) => {
        const { escalations, total } = await escalationService.listEscalations({
            status: args.status,
            role: args.role,
            type: args.type,
            priority: args.priority,
            limit: args.limit,
            offset: args.offset,
        });
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        total,
                        count: escalations.length,
                        escalations: escalations.map((e) => ({
                            id: e.id,
                            type: e.type,
                            subtype: e.subtype,
                            role: e.role,
                            priority: e.priority,
                            status: e.status,
                            description: e.description,
                            workflow_type: e.workflow_type,
                            assigned_to: e.assigned_to,
                            created_at: e.created_at,
                        })),
                    }),
                }],
        };
    });
    // mirrors GET /api/escalations/stats
    server.registerTool('get_escalation_stats', {
        title: 'Get Escalation Stats',
        description: 'Aggregated escalation statistics: pending, claimed, created, ' +
            'resolved counts with breakdown by role and type.',
        inputSchema: schemas_1.getEscalationStatsSchema,
    }, async (args) => {
        const stats = await escalationService.getEscalationStats(undefined, args.period);
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify(stats),
                }],
        };
    });
    // mirrors POST /api/escalations/:id/claim
    server.registerTool('claim_escalation', {
        title: 'Claim Escalation',
        description: 'Claim an escalation for a time-boxed lock. The escalation is ' +
            'reserved so no one else picks it up. If the claim expires, ' +
            'the escalation returns to the queue.',
        inputSchema: schemas_1.claimEscalationSchema,
    }, async (args) => {
        const escalation = await escalationService.getEscalation(args.id);
        if (!escalation) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'Escalation not found' }) }],
                isError: true,
            };
        }
        const result = await escalationService.claimEscalation(args.id, 'lt-system', args.duration_minutes);
        if (!result) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'Escalation not available for claim' }) }],
                isError: true,
            };
        }
        return {
            content: [{ type: 'text', text: JSON.stringify(result) }],
        };
    });
    // mirrors POST /api/escalations/release-expired
    server.registerTool('release_expired_claims', {
        title: 'Release Expired Claims',
        description: 'Release all escalation claims that exceeded their lock duration. ' +
            'Expired escalations return to the available queue.',
        inputSchema: schemas_1.releaseExpiredClaimsSchema,
    }, async (_args) => {
        const released = await escalationService.releaseExpiredClaims();
        return {
            content: [{ type: 'text', text: JSON.stringify({ released }) }],
        };
    });
    // mirrors POST /api/escalations/bulk-triage
    server.registerTool('bulk_triage', {
        title: 'Bulk Triage',
        description: 'Resolve escalations for triage and start mcpTriage workflows. ' +
            'Each escalation is marked resolved with needsTriage=true, then ' +
            'a triage workflow is started to remediate the issue.',
        inputSchema: schemas_1.bulkTriageSchema,
    }, async (args) => {
        const resolved = await escalationService.bulkResolveForTriage(args.ids, args.hint);
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        triaged: resolved.length,
                        escalation_ids: resolved.map((e) => e.id),
                    }),
                }],
        };
    });
}
