"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_store_1 = require("../../../lib/config/config-store");
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.')
        .option('--project <id>', 'Report the view for one declared monorepo project.')
        .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 options = commandInstance.opts();
        const resolvedConfig = (0, config_store_1.readResolvedConfig)(repositoryContext, {
            projectId: options.project,
            currentDirectory: process.cwd(),
        }).resolved;
        const result = (0, planning_navigation_1.inventoryPlanning)({
            repositoryRoot: repositoryContext.root,
            rootPath: options.root ?? resolvedConfig.values.planning_root,
            projectContext: resolvedConfig.projectContext,
        });
        (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,
                workOverviewPaths: result.workOverviewPaths,
                sliceOverviewPaths: result.sliceOverviewPaths,
                phaseOverviewPaths: result.phaseOverviewPaths,
                milestoneOverviewPaths: result.milestoneOverviewPaths,
                taskDefinitionPaths: result.taskDefinitionPaths,
                implementationSummaryPaths: result.implementationSummaryPaths,
                reconciliationPaths: result.reconciliationPaths,
                discovery: result.discovery,
                ...(resolvedConfig.projectContext.projects.length > 0
                    ? { projectContext: resolvedConfig.projectContext }
                    : {}),
                workDiscovery: result.workDiscovery,
                legacyCompatibility: result.legacyCompatibility,
                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);
}
