"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMetadataInventoryCommand = createMetadataInventoryCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const metadata_sync_and_inventory_1 = require("../../../lib/planning/metadata-sync-and-inventory");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
async function createMetadataInventoryCommand() {
    const { Command } = await import("commander");
    const command = new Command("inventory");
    command
        .description("Report metadata state, helper coverage, and update opportunities without writing files.")
        .option("--root <path>", "Repository-relative path to the planning tree to inventory.", ".day-shift/planning")
        .action((_, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(commandInstance));
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String((0, global_format_1.getGlobalFormat)(commandInstance))}`]);
        }
        const repositoryContext = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const options = commandInstance.opts();
        const result = (0, metadata_sync_and_inventory_1.inventoryMetadataState)({
            repositoryRoot: repositoryContext.root,
            rootPath: options.root
        });
        (0, output_1.emitSuccess)({
            command: "metadata inventory",
            format,
            text: (0, metadata_sync_and_inventory_1.renderMetadataInventoryResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                inventoryRoot: result.inventoryRoot,
                taskDefinitionsScanned: result.taskDefinitionsScanned,
                implementationSummariesCompleted: result.implementationSummariesCompleted,
                reconciliationsCompleted: result.reconciliationsCompleted,
                helperCoverage: result.helperCoverage,
                updateOpportunities: result.updateOpportunities
            }
        });
    });
    return command;
}
