import { getNextActions } from "../suggestions.js";
import { inferTaskPhaseKey, resolveCanonicalPhase, resolvePhaseScheduleRelation } from "../phase-resolution.js";
import { isWishlistIntakeTask } from "../wishlist/wishlist-intake.js";
function phaseAlignmentForTask(task, canonicalPhaseKey) {
    const taskPhaseKey = inferTaskPhaseKey(task);
    if (canonicalPhaseKey === null) {
        return { phaseAligned: null, taskPhaseKey, phaseScheduleRelation: "unknown" };
    }
    if (taskPhaseKey === null) {
        return { phaseAligned: null, taskPhaseKey: null, phaseScheduleRelation: "unknown" };
    }
    const relation = resolvePhaseScheduleRelation({
        taskPhaseKey,
        workspacePhaseKey: canonicalPhaseKey
    });
    const phaseAligned = relation === "current";
    return { phaseAligned, taskPhaseKey, phaseScheduleRelation: relation };
}
export function buildQueueHealthReport(args) {
    const phaseCtx = resolveCanonicalPhase({
        effectiveConfig: args.effectiveConfig,
        workspaceStatus: args.workspaceStatus
    });
    const kit = args.effectiveConfig?.kit;
    const kitObj = kit !== null && typeof kit === "object" && !Array.isArray(kit) ? kit : undefined;
    const labelRaw = kitObj?.currentPhaseLabel;
    const phaseLabelFromConfig = typeof labelRaw === "string" && labelRaw.trim().length > 0 ? labelRaw.trim() : null;
    const suggestion = getNextActions(args.tasks);
    const completedIds = new Set(args.tasks.filter((t) => t.status === "completed").map((t) => t.id));
    const readyTaskSummaries = [];
    let misalignedPhaseCount = 0;
    let scheduledFuturePhaseCount = 0;
    let blockedByDependenciesCount = 0;
    let healthyReadyCount = 0;
    for (const task of suggestion.readyQueue) {
        if (isWishlistIntakeTask(task)) {
            continue;
        }
        const deps = task.dependsOn ?? [];
        const unmetDependencies = deps.filter((depId) => !completedIds.has(depId));
        const blockedByDependencies = unmetDependencies.length > 0;
        const { phaseAligned, taskPhaseKey, phaseScheduleRelation } = phaseAlignmentForTask(task, phaseCtx.canonicalPhaseKey);
        if (phaseAligned === false && phaseScheduleRelation === "future") {
            scheduledFuturePhaseCount += 1;
        }
        else if (phaseAligned === false) {
            misalignedPhaseCount += 1;
        }
        if (blockedByDependencies) {
            blockedByDependenciesCount += 1;
        }
        const healthy = blockedByDependencies === false &&
            (phaseScheduleRelation === "current" ||
                phaseScheduleRelation === "future" ||
                phaseScheduleRelation === "unknown");
        if (healthy) {
            healthyReadyCount += 1;
        }
        readyTaskSummaries.push({
            taskId: task.id,
            title: task.title,
            phaseAligned,
            taskPhaseKey,
            canonicalPhaseKey: phaseCtx.canonicalPhaseKey,
            phaseScheduleRelation,
            blockedByDependencies,
            unmetDependencies
        });
    }
    return {
        schemaVersion: 1,
        scope: "tasks-only",
        canonicalPhase: {
            ...phaseCtx,
            phaseLabelFromConfig
        },
        readyTaskSummaries,
        summary: {
            readyCount: readyTaskSummaries.length,
            misalignedPhaseCount,
            scheduledFuturePhaseCount,
            blockedByDependenciesCount,
            healthyReadyCount
        }
    };
}
/** Hints for list-tasks / filtering; same dependency + phase rules as queue-health ready rows. */
export function buildQueueHintsForTasks(args) {
    const phaseCtx = resolveCanonicalPhase({
        effectiveConfig: args.effectiveConfig,
        workspaceStatus: args.workspaceStatus
    });
    const completedIds = new Set(args.tasks.filter((t) => t.status === "completed").map((t) => t.id));
    return args.taskRows.map((task) => {
        const deps = task.dependsOn ?? [];
        const unmetDependencies = deps.filter((depId) => !completedIds.has(depId));
        const blockedByDependencies = task.status === "ready" && !isWishlistIntakeTask(task) ? unmetDependencies.length > 0 : false;
        let phaseAligned = null;
        let phaseScheduleRelation = "unknown";
        if (task.status === "ready" && !isWishlistIntakeTask(task)) {
            const r = phaseAlignmentForTask(task, phaseCtx.canonicalPhaseKey);
            phaseAligned = r.phaseAligned;
            phaseScheduleRelation = r.phaseScheduleRelation;
        }
        return {
            taskId: task.id,
            phaseAligned,
            phaseScheduleRelation,
            blockedByDependencies,
            unmetDependencies: task.status === "ready" && !isWishlistIntakeTask(task) ? unmetDependencies : []
        };
    });
}
