import { humanGateAgeMs, isHumanGateStatus } from "../human-gate.js";
import { inferTaskPhaseKey } from "../phase-resolution.js";
import { projectDashboardTaskRow } from "../task-read-projections.js";
function priorityRank(task) {
    if (task.priority === "P1")
        return 0;
    if (task.priority === "P2")
        return 1;
    if (task.priority === "P3")
        return 2;
    return 99;
}
function taskRecency(task) {
    const n = Date.parse(task.updatedAt || task.createdAt || "");
    return Number.isFinite(n) ? n : 0;
}
function sortHumanGateTasks(a, b) {
    const pr = priorityRank(a) - priorityRank(b);
    if (pr !== 0)
        return pr;
    const recent = taskRecency(b) - taskRecency(a);
    if (recent !== 0)
        return recent;
    return a.id.localeCompare(b.id);
}
export function buildDashboardHumanGatesSummary(tasks, currentKitPhase, enrich, topLimit = 15) {
    const gated = tasks.filter((t) => isHumanGateStatus(t.status));
    const scoped = currentKitPhase && currentKitPhase.trim().length > 0
        ? gated.filter((t) => inferTaskPhaseKey(t) === currentKitPhase.trim())
        : gated;
    const sorted = [...scoped].sort(sortHumanGateTasks);
    const top = sorted.slice(0, topLimit).map((task) => {
        const gate = task.metadata?.humanGate;
        const base = projectDashboardTaskRow(task, enrich);
        return {
            ...base,
            status: task.status,
            gateKind: task.status,
            ageMs: gate ? humanGateAgeMs(gate) : 0,
            enteredAt: gate?.enteredAt ?? null,
            requestedDecision: gate?.requestedDecision ?? null,
            owner: gate?.owner ?? null,
            reason: gate?.reason ?? null
        };
    });
    return {
        schemaVersion: 1,
        phaseKey: currentKitPhase,
        count: scoped.length,
        top
    };
}
