/**
 * Seed diagnostics route + helpers.
 *
 * Exposes a read-only `/state/seed-status` endpoint returning the safe summary
 * of the last `applySeed` invocation. The response intentionally contains only
 * bounded labels: enabled flag, enabled domain ids, applied-at ISO timestamp,
 * per-domain inserted/skipped counts, and an optional sanitized error reason
 * code. No row contents, no secrets, no raw config, no provider/path/audit
 * internals.
 */
import { writeJson } from "./publicResponse.js";
export async function handleSeedStatusRoute(context) {
    const { app, request, response, url } = context;
    if (request.method !== "GET" || url.pathname !== "/state/seed-status") {
        return false;
    }
    writeJson(response, 200, formatSeedStatus(app.seedStatus));
    return true;
}
export function formatSeedStatus(holder) {
    const result = holder.current;
    if (!result) {
        return {
            enabled: false,
            appliedAt: null,
            enabledDomains: [],
            counts: emptyCounts(),
            ...(holder.lastError ? { lastError: holder.lastError } : {}),
        };
    }
    const enabledDomains = [];
    if (result.domains.runs.inserted + result.domains.runs.skipped > 0)
        enabledDomains.push("runs");
    if (result.domains.runtimeEvents.inserted +
        result.domains.runtimeEvents.skipped >
        0)
        enabledDomains.push("runtimeEvents");
    if (result.domains.proofEvidence.inserted +
        result.domains.proofEvidence.skipped >
        0)
        enabledDomains.push("proofEvidence");
    if (result.domains.permissionActivity.inserted +
        result.domains.permissionActivity.skipped >
        0)
        enabledDomains.push("permissionActivity");
    if (result.domains.repositories.inserted + result.domains.repositories.skipped >
        0)
        enabledDomains.push("repositories");
    if (result.domains.onboarding.inserted + result.domains.onboarding.skipped >
        0)
        enabledDomains.push("onboarding");
    if (result.domains.settings.inserted + result.domains.settings.skipped > 0)
        enabledDomains.push("settings");
    if (result.domains.reviewQueue.inserted +
        result.domains.reviewQueue.skipped >
        0 ||
        result.domains.reviewDecisions.inserted +
            result.domains.reviewDecisions.skipped >
            0)
        enabledDomains.push("reviewQueue");
    return {
        enabled: result.enabled,
        appliedAt: result.appliedAt,
        enabledDomains,
        counts: {
            workspace: { ...result.domains.workspace },
            runs: { ...result.domains.runs },
            runtimeEvents: { ...result.domains.runtimeEvents },
            proofEvidence: { ...result.domains.proofEvidence },
            permissionActivity: { ...result.domains.permissionActivity },
            repositories: { ...result.domains.repositories },
            onboarding: { ...result.domains.onboarding },
            settings: { ...result.domains.settings },
            reviewQueue: { ...result.domains.reviewQueue },
            reviewDecisions: { ...result.domains.reviewDecisions },
        },
        ...(holder.lastError ? { lastError: holder.lastError } : {}),
    };
}
function emptyCounts() {
    return {
        workspace: { inserted: 0, skipped: 0 },
        runs: { inserted: 0, skipped: 0 },
        runtimeEvents: { inserted: 0, skipped: 0 },
        proofEvidence: { inserted: 0, skipped: 0 },
        permissionActivity: { inserted: 0, skipped: 0 },
        repositories: { inserted: 0, skipped: 0 },
        onboarding: { inserted: 0, skipped: 0 },
        settings: { inserted: 0, skipped: 0 },
        reviewQueue: { inserted: 0, skipped: 0 },
        reviewDecisions: { inserted: 0, skipped: 0 },
    };
}
// Silence unused-export warnings if a consumer mocks the holder type.
void null;
