"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPhaseReviewCommand = createPhaseReviewCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const planning_review_1 = require("../../../lib/planning/planning-review");
const review_output_contracts_1 = require("../../../lib/planning/review-output-contracts");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
async function createPhaseReviewCommand() {
    const { Command } = await import("commander");
    const command = new Command("review");
    command
        .description("Review a phase overview as a read-only, no-write evidence surface against planning readiness, pre-scaffold contract-impact coverage, or downstream milestone and reconciliation evidence.")
        .requiredOption("--phase-overview <path>", "Repository-relative path to the phase overview to review.")
        .option("--metadata-posture <value>", "Lifecycle metadata posture: read-only or sync-on-clean-review.", "read-only")
        .option("--reviewed-at <timestamp>", "ISO-8601 review time; defaults to command time.")
        .option("--review-mode <value>", "Review selection: auto or readiness. Use readiness to establish milestone authoring handoff evidence even when partial milestone pairs already exist.", "auto")
        .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();
        if (options.reviewMode !== "auto" && options.reviewMode !== "readiness") {
            throw (0, errors_1.createUsageError)("--review-mode must be one of: auto, readiness.", [`Received: ${String(options.reviewMode)}`]);
        }
        const result = (0, planning_review_1.reviewPhasePlanning)(repositoryContext.root, {
            phaseOverviewPath: options.phaseOverview,
            metadataPosture: options.metadataPosture,
            reviewedAt: options.reviewedAt,
            reviewMode: options.reviewMode === "readiness" ? "readiness" : undefined
        });
        (0, output_1.emitSuccess)({
            command: "phase review",
            format,
            text: (0, planning_review_1.renderPhasePlanningReviewResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                readOnly: true,
                writeCapable: false,
                noWriteBoundary: (0, review_output_contracts_1.getReviewOutputContract)("phase review").noWriteBoundary,
                reviewOutputContract: (0, review_output_contracts_1.getReviewOutputContract)("phase review"),
                reviewMode: result.reviewMode,
                metadataPosture: result.metadataPosture,
                reviewedAt: result.reviewedAt,
                phaseOverviewPath: result.phaseOverviewPath,
                milestoneOverviewPaths: result.milestoneOverviewPaths,
                reconciledMilestonePaths: result.reconciledMilestonePaths,
                blockingIssues: result.blockingIssues,
                phaseAuthoringBlockingIssues: result.phaseAuthoringBlockingIssues,
                warnings: result.warnings,
                evidenceLanes: result.evidenceLanes,
                administrativeStatusDrift: result.administrativeStatusDrift,
                metadataStatusTransitions: result.metadataStatusTransitions,
                reconciliationEvidence: result.reconciliationEvidence,
                phaseReviewReceipt: result.phaseReviewReceipt,
                phaseAuthoringReceipt: result.phaseAuthoringReceipt,
                contractImpactCoverage: result.contractImpactCoverage,
                materializedScaffoldPaths: result.materializedScaffoldPaths,
                normalizedScaffoldEvidence: result.normalizedScaffoldEvidence,
                scaffoldProjectionDrift: result.scaffoldProjectionDrift,
                scaffoldMigration: result.scaffoldMigration,
                metadataHygieneFollowUps: result.metadataHygieneFollowUps,
                recoveryGuidance: result.recoveryGuidance
            },
            findings: [...result.findings]
        });
    });
    return command;
}
