"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bulkClaimEscalations = bulkClaimEscalations;
exports.bulkAssignEscalations = bulkAssignEscalations;
exports.bulkEscalateToRole = bulkEscalateToRole;
exports.bulkResolveForTriage = bulkResolveForTriage;
const db_1 = require("../../lib/db");
const sql_1 = require("./sql");
/**
 * Bulk claim escalations for a user.
 * Items already claimed by another active user are skipped.
 */
async function bulkClaimEscalations(ids, userId, durationMinutes = 30) {
    if (ids.length === 0)
        return { claimed: 0, skipped: 0 };
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(sql_1.BULK_CLAIM, [userId, durationMinutes, ids]);
    const claimed = rowCount ?? 0;
    return { claimed, skipped: ids.length - claimed };
}
/**
 * Bulk assign escalations to a specific user (admin action).
 * Items already claimed by another active user are skipped.
 */
async function bulkAssignEscalations(ids, targetUserId, durationMinutes = 30) {
    if (ids.length === 0)
        return { assigned: 0, skipped: 0 };
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(sql_1.BULK_ASSIGN, [targetUserId, durationMinutes, ids]);
    const assigned = rowCount ?? 0;
    return { assigned, skipped: ids.length - assigned };
}
/**
 * Bulk reassign escalations to a different role.
 * Clears assignment on all affected rows.
 */
async function bulkEscalateToRole(ids, targetRole) {
    if (ids.length === 0)
        return 0;
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(sql_1.BULK_ESCALATE_TO_ROLE, [targetRole, ids]);
    return rowCount ?? 0;
}
/**
 * Bulk resolve escalations for AI triage.
 * Returns full records so the caller can start triage workflows.
 */
async function bulkResolveForTriage(ids, hint) {
    if (ids.length === 0)
        return [];
    const pool = (0, db_1.getPool)();
    const resolverPayload = JSON.stringify({
        _lt: { needsTriage: true, ...(hint ? { hint } : {}) },
    });
    const { rows } = await pool.query(sql_1.BULK_RESOLVE_FOR_TRIAGE, [resolverPayload, ids]);
    return rows;
}
