"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createValidationSmokeCommand = createValidationSmokeCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const doctor_1 = require("../../../lib/doctor");
const global_format_1 = require("../global-format");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
async function createValidationSmokeCommand() {
    const { Command } = await import("commander");
    const command = new Command("smoke");
    command
        .description("Run a concise read-only, no-write planning validation summary and advisory safe-next-step report.")
        .option("--scope <scope>", "Validation smoke scope. Currently only 'planning' is supported.", "planning")
        .option("--artifact <path>", "Optional repository-relative planning artifact to include in next-action guidance.")
        .option("--action <descriptor>", "Optional exact authoritative command descriptor id used to classify the unchanged focused findings.")
        .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 options = commandInstance.opts();
        const repositoryRoot = getRootCommand(commandInstance).configRepositoryContext?.root;
        if (options.scope !== "planning") {
            throw (0, errors_1.createUsageError)("Validation smoke currently supports only the 'planning' scope.", [`Received: ${String(options.scope)}`]);
        }
        if (options.action !== undefined && options.artifact === undefined) {
            throw (0, errors_1.createUsageError)("--action requires one explicit --artifact for focused planning validation.", ["No validation, repair, metadata, status, or planning-artifact write was performed."]);
        }
        const actionDescriptor = options.action === undefined
            ? null
            : (0, output_1.getRegisteredCommandDescriptor)(String(options.action).trim());
        if (options.action !== undefined
            && (actionDescriptor === null || actionDescriptor.availability !== "callable")) {
            throw (0, errors_1.createUsageError)(`Unknown callable action descriptor '${String(options.action)}'.`, ["Use an exact callable command id from the authoritative command descriptor registry.", "No files were changed."]);
        }
        const report = (0, doctor_1.runPlanningValidationSmoke)({
            artifactPath: options.artifact,
            repositoryRoot,
            actionId: options.action
        });
        (0, output_1.emitSuccess)({
            command: "validation smoke",
            format,
            text: (0, doctor_1.renderPlanningValidationSmokeReport)(report),
            data: {
                repositoryRoot: report.repositoryRoot,
                detectionSource: report.detectionSource,
                runtimeProvenance: (0, runtime_contract_1.getRuntimeProvenance)(),
                scope: report.scope,
                planningRootPath: report.planningRootPath,
                outputContracts: (0, output_1.getValidationCommandOutputContracts)(),
                readOnlyFailureContract: (0, output_1.getValidationReadOnlyFailureContract)(),
                evidenceSourceInventory: report.evidenceSourceInventory,
                specializedReviewBoundaries: report.specializedReviewBoundaries,
                aggregationContract: report.aggregationContract,
                checks: report.checks,
                summary: report.summary,
                readinessStatus: report.readinessStatus,
                inventoryFindings: report.inventoryFindings,
                crossArtifactAlignment: report.crossArtifactAlignment,
                baseline: report.baseline,
                gaps: report.gaps,
                nextActions: report.nextActions,
                nextAction: report.nextAction,
                ...(report.actionRelevance === null ? {} : { actionRelevance: report.actionRelevance })
            },
            findings: [...report.findings]
        });
    });
    return command;
}
function getRootCommand(commandInstance) {
    let current = commandInstance;
    while (current?.parent) {
        current = current.parent;
    }
    return current ?? commandInstance;
}
