/**
 * coverage-audit command — cross-references the product feature registry
 * against task files to produce a documentation coverage audit.
 *
 */
import { InMemoryPluginRegistry } from "../_vendor/ailf-core/index.js";
import { Command } from "commander";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { countReferencedDocs, formatCoverageConsole, formatCoverageMarkdown, runCoverageAudit, } from "../pipeline/coverage-audit.js";
import { createLiteracyModeBase } from "../pipeline/compiler/mode-bases/index.js";
import { createSanityLiteracyPreset } from "../pipeline/compiler/presets/index.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..", "..");
export function createCoverageAuditCommand() {
    return new Command("coverage")
        .description("Run documentation coverage audit against feature registry")
        .option("-f, --format <fmt>", "Output format: table, md, markdown, json", "table")
        .action(async (opts) => {
        // Build a registry with mode base + preset so coverage audit works
        // even when config/features.ts is empty (preset is source of truth).
        const registry = new InMemoryPluginRegistry();
        registry.registerModeBase(createLiteracyModeBase());
        registry.registerPreset(createSanityLiteracyPreset({ rootDir: ROOT }));
        const report = runCoverageAudit(ROOT, { registry });
        if (!report) {
            console.error("❌ Coverage audit failed. Ensure config/features.yaml exists and is valid.");
            process.exit(1);
        }
        const isMarkdown = opts.format === "md" || opts.format === "markdown";
        if (opts.format === "json") {
            console.log(JSON.stringify(report, null, 2));
        }
        else if (isMarkdown) {
            console.log(formatCoverageMarkdown(report));
        }
        else {
            console.log(formatCoverageConsole(report));
        }
        if (opts.format !== "json" && !isMarkdown) {
            const docStats = countReferencedDocs(ROOT);
            console.log("DOCUMENT UTILIZATION:");
            console.log(`  ${docStats.total} unique document slugs referenced across evaluation tasks`);
            console.log("");
        }
    });
}
