"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 });
const express_1 = require("express");
const auth_1 = require("../modules/auth");
const api = __importStar(require("../api/roles"));
const router = (0, express_1.Router)();
/**
 * GET /api/roles
 * List all distinct roles known to the system.
 */
router.get('/', async (_req, res) => {
    const result = await api.listRoles();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/roles/details
 * List all roles with usage counts.
 */
router.get('/details', async (_req, res) => {
    const result = await api.listRolesWithDetails();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/roles
 * Create a standalone role. Requires admin.
 * Body: { role: string }
 */
router.post('/', auth_1.requireAdmin, async (req, res) => {
    const { role } = req.body || {};
    const result = await api.createRole({ role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/roles/escalation-chains
 * Get all escalation chain pairs.
 */
router.get('/escalation-chains', async (_req, res) => {
    const result = await api.getEscalationChains();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/roles/escalation-chains
 * Add a single escalation chain entry. Requires admin.
 * Body: { source_role: string, target_role: string }
 */
router.post('/escalation-chains', auth_1.requireAdmin, async (req, res) => {
    const { source_role, target_role } = req.body || {};
    const result = await api.addEscalationChain({ source_role, target_role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/roles/escalation-chains
 * Remove a single escalation chain entry. Requires admin.
 * Body: { source_role: string, target_role: string }
 */
router.delete('/escalation-chains', auth_1.requireAdmin, async (req, res) => {
    const { source_role, target_role } = req.body || {};
    const result = await api.removeEscalationChain({ source_role, target_role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Parameterized routes (must come AFTER named routes) ───────────────────
/**
 * GET /api/roles/:role/escalation-targets
 * Get allowed escalation targets for a specific role.
 */
router.get('/:role/escalation-targets', async (req, res) => {
    const result = await api.getEscalationTargets({ role: req.params.role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/roles/:role/escalation-targets
 * Replace escalation targets for a role. Requires admin.
 * Body: { targets: string[] }
 */
router.put('/:role/escalation-targets', auth_1.requireAdmin, async (req, res) => {
    const { targets } = req.body || {};
    const result = await api.replaceEscalationTargets({ role: req.params.role, targets });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/roles/:role
 * Delete a role if it has no references. Requires admin.
 */
router.delete('/:role', auth_1.requireAdmin, async (req, res) => {
    const result = await api.deleteRole({ role: req.params.role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
