"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPlanningInventoryCommand = createPlanningInventoryCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const planning_navigation_1 = require("../../../lib/planning/planning-navigation");
const config_contract_1 = require("../../../lib/config/config-contract");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
async function createPlanningInventoryCommand() {
    const { Command } = await import("commander");
    const command = new Command("inventory");
    command
        .description("Report observed planning artifacts and paired execution/review evidence for a selected scope.")
        .option("--root <path>", "Repository-relative planning root or subdirectory to inspect. Defaults to configured planning_root.")
        .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 = resolveRepositoryContext(commandInstance);
        const resolvedConfig = (0, config_contract_1.resolveDayShiftConfig)({ repositoryContext });
        const options = commandInstance.opts();
        const result = (0, planning_navigation_1.inventoryPlanning)({
            repositoryRoot: repositoryContext.root,
            rootPath: options.root ?? resolvedConfig.values.planning_root
        });
        (0, output_1.emitSuccess)({
            command: "planning inventory",
            format,
            text: (0, planning_navigation_1.renderPlanningInventoryResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                scopePath: result.scopePath,
                scopeKind: result.scopeKind,
                sliceOverviewPaths: result.sliceOverviewPaths,
                phaseOverviewPaths: result.phaseOverviewPaths,
                milestoneOverviewPaths: result.milestoneOverviewPaths,
                taskDefinitionPaths: result.taskDefinitionPaths,
                implementationSummaryPaths: result.implementationSummaryPaths,
                reconciliationPaths: result.reconciliationPaths,
                discovery: result.discovery,
                findings: result.findings
            }
        });
    });
    return command;
}
function resolveRepositoryContext(commandInstance) {
    const rootCommand = commandInstance?.parent?.parent ?? commandInstance;
    if (rootCommand?.configRepositoryContext) {
        return rootCommand.configRepositoryContext;
    }
    return (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
}
