"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.getEscalation = getEscalation;
exports.getEscalationsByWorkflowId = getEscalationsByWorkflowId;
exports.escalateToRole = escalateToRole;
const escalationService = __importStar(require("../../services/escalation"));
const userService = __importStar(require("../../services/user"));
const roleService = __importStar(require("../../services/role"));
// ── Single-escalation routes ───────────────────────────────────────────────
/**
 * Get a single escalation by ID.
 *
 * Non-superadmin users must hold the escalation's assigned role.
 *
 * @param input.id — escalation UUID
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: <escalation record> }` or 403/404
 */
async function getEscalation(input, auth) {
    try {
        const escalation = await escalationService.getEscalation(input.id);
        if (!escalation) {
            return { status: 404, error: 'Escalation not found' };
        }
        const isSuperAdminUser = await userService.isSuperAdmin(auth.userId);
        if (!isSuperAdminUser) {
            const userHasRole = await userService.hasRole(auth.userId, escalation.role);
            if (!userHasRole) {
                return { status: 403, error: 'Not authorized to view this escalation' };
            }
        }
        return { status: 200, data: escalation };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * List all escalations for a given workflow ID.
 *
 * @param input.workflowId — HotMesh workflow ID
 * @returns `{ status: 200, data: { escalations } }`
 */
async function getEscalationsByWorkflowId(input) {
    try {
        const escalations = await escalationService.getEscalationsByWorkflowId(input.workflowId);
        return { status: 200, data: { escalations } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Route a pending escalation to a different role.
 *
 * The user must be authorized to escalate from the current role to the
 * target role (checked via escalation chain configuration).
 *
 * @param input.id — escalation UUID
 * @param input.targetRole — destination role
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: <updated escalation> }` or 403/404/409
 */
async function escalateToRole(input, auth) {
    try {
        const { id, targetRole } = input;
        if (!targetRole || typeof targetRole !== 'string') {
            return { status: 400, error: 'targetRole is required' };
        }
        const escalation = await escalationService.getEscalation(id);
        if (!escalation) {
            return { status: 404, error: 'Escalation not found' };
        }
        if (escalation.status !== 'pending') {
            return { status: 409, error: 'Escalation is not pending' };
        }
        const canEscalate = await roleService.canEscalateTo(auth.userId, escalation.role, targetRole);
        if (!canEscalate) {
            return { status: 403, error: 'Not authorized to escalate to this role' };
        }
        const updated = await escalationService.escalateToRole(id, targetRole);
        if (!updated) {
            return { status: 409, error: 'Escalation could not be updated' };
        }
        return { status: 200, data: updated };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
