"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPlanningGapReportCommand = createPlanningGapReportCommand;
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 createPlanningGapReportCommand() {
    const { Command } = await import("commander");
    const command = new Command("gap-report");
    command
        .description("Report expected-versus-observed planning gaps for a selected planning artifact or scope.")
        .option("--scope <path>", "Repository-relative planning artifact or planning directory to inspect. Defaults to configured planning_root.")
        .option("--artifact <path>", "Alias for --scope when selecting a repository-relative planning artifact.")
        .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 scopePath = resolveGapReportScopeOption(options, resolvedConfig.values.planning_root);
        const result = (0, planning_navigation_1.gapReportPlanning)({
            repositoryRoot: repositoryContext.root,
            scopePath
        });
        (0, output_1.emitSuccess)({
            command: "planning gap-report",
            format,
            text: (0, planning_navigation_1.renderPlanningGapReportResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                scopePath: result.scopePath,
                scopeKind: result.scopeKind,
                gaps: result.gaps
            }
        });
    });
    return command;
}
function resolveGapReportScopeOption(options, defaultScope) {
    if (options.scope !== undefined
        && options.artifact !== undefined
        && options.scope !== options.artifact) {
        throw (0, errors_1.createUsageError)("planning gap-report received conflicting selectors.", [
            `--scope: ${options.scope}`,
            `--artifact: ${options.artifact}`,
            "Pass only one selector, or pass the same path to both options."
        ]);
    }
    return options.artifact ?? options.scope ?? defaultScope;
}
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);
}
