"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.listEscalations = listEscalations;
exports.listAvailableEscalations = listAvailableEscalations;
exports.listDistinctTypes = listDistinctTypes;
exports.getEscalationStats = getEscalationStats;
const escalationService = __importStar(require("../../services/escalation"));
const helpers_1 = require("./helpers");
// ── List routes ────────────────────────────────────────────────────────────
/**
 * List escalations with optional filters.
 *
 * Results are scoped to the authenticated user's roles unless the user
 * is a superadmin (who sees all roles).
 *
 * @param input.status — filter by `pending`, `resolved`, or `cancelled`
 * @param input.role — filter by assigned role
 * @param input.type — filter by workflow type
 * @param input.subtype — filter by subtype
 * @param input.assigned_to — filter by assigned user ID
 * @param input.priority — filter by priority (1–4)
 * @param input.limit — max results (default: 50)
 * @param input.offset — pagination offset
 * @param input.sort_by — column to sort by (e.g. `created_at`, `priority`)
 * @param input.order — `asc` or `desc`
 * @param auth — authenticated user context (required for role scoping)
 * @returns `{ status: 200, data: { escalations, total } }`
 */
async function listEscalations(input, auth) {
    try {
        const visibleRoles = await (0, helpers_1.getVisibleRoles)(auth.userId);
        if (visibleRoles && visibleRoles.length === 0) {
            return { status: 200, data: { escalations: [], total: 0 } };
        }
        const result = await escalationService.listEscalations({
            status: input.status,
            role: input.role,
            type: input.type,
            subtype: input.subtype,
            assigned_to: input.assigned_to,
            priority: input.priority,
            limit: input.limit,
            offset: input.offset,
            sort_by: input.sort_by,
            order: input.order,
            visibleRoles,
        });
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * List escalations available for claim (pending and not actively claimed).
 *
 * Similar to `listEscalations` but excludes escalations with active claims.
 * Scoped to the authenticated user's roles.
 *
 * @param input.role — filter by role
 * @param input.type — filter by workflow type
 * @param input.subtype — filter by subtype
 * @param input.priority — filter by priority (1–4)
 * @param input.limit — max results (default: 50)
 * @param input.offset — pagination offset
 * @param input.sort_by — column to sort by
 * @param input.order — `asc` or `desc`
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: { escalations, total } }`
 */
async function listAvailableEscalations(input, auth) {
    try {
        const visibleRoles = await (0, helpers_1.getVisibleRoles)(auth.userId);
        if (visibleRoles && visibleRoles.length === 0) {
            return { status: 200, data: { escalations: [], total: 0 } };
        }
        const result = await escalationService.listAvailableEscalations({
            role: input.role,
            type: input.type,
            subtype: input.subtype,
            priority: input.priority,
            limit: input.limit,
            offset: input.offset,
            sort_by: input.sort_by,
            order: input.order,
            visibleRoles,
        });
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * List all distinct escalation type values.
 *
 * @returns `{ status: 200, data: { types: string[] } }`
 */
async function listDistinctTypes() {
    try {
        const types = await escalationService.listDistinctTypes();
        return { status: 200, data: { types } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Get aggregate escalation statistics scoped to the user's roles.
 *
 * @param input.period — time window (`1h`, `24h`, `7d`, `30d`)
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: { pending, claimed, created, resolved, by_role, by_type } }`
 */
async function getEscalationStats(input, auth) {
    try {
        const visibleRoles = await (0, helpers_1.getVisibleRoles)(auth.userId);
        if (visibleRoles && visibleRoles.length === 0) {
            return {
                status: 200,
                data: {
                    pending: 0,
                    claimed: 0,
                    created: 0,
                    resolved: 0,
                    by_role: [],
                    by_type: [],
                },
            };
        }
        const stats = await escalationService.getEscalationStats(visibleRoles, input.period);
        return { status: 200, data: stats };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
