"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createImplementationSummaryReviewCommand = createImplementationSummaryReviewCommand;
const errors_1 = require("../../../lib/errors");
const implementation_summary_reconciliation_1 = require("../../../lib/planning/implementation-summary-reconciliation");
const output_1 = require("../../../lib/output");
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 createImplementationSummaryReviewCommand() {
    const { Command } = await import("commander");
    const command = new Command("review");
    command
        .description("Evaluate a paired implementation-summary artifact as a read-only, no-write evidence surface without mutating lifecycle metadata.")
        .requiredOption("--task-definition <path>", "Repository-relative path to the task-definition.md file whose paired implementation summary should be reviewed.")
        .option("--review-result-path <path>", "Unbound caller path used only to derive lifecycle argv; a run-bound review uses and exposes its exact manifest-owned evidence path instead.")
        .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();
        const manifestReviewResultPath = (0, output_1.getSelectedLifecycleRunCaptureEvidencePath)("ok");
        const reviewResultPath = manifestReviewResultPath ?? options.reviewResultPath;
        const result = (0, implementation_summary_reconciliation_1.reviewImplementationSummary)(repositoryContext.root, {
            taskDefinitionPath: options.taskDefinition,
            reviewResultPath
        });
        (0, output_1.emitSuccess)({
            command: "implementation-summary review",
            format,
            text: (0, implementation_summary_reconciliation_1.renderImplementationSummaryReviewResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                readOnly: true,
                writeCapable: false,
                noWriteBoundary: (0, review_output_contracts_1.getReviewOutputContract)("implementation-summary review").noWriteBoundary,
                reviewOutputContract: (0, review_output_contracts_1.getReviewOutputContract)("implementation-summary review"),
                taskDefinitionPath: result.taskDefinitionPath,
                implementationSummaryPath: result.implementationSummaryPath,
                implementationStatus: result.implementationStatus,
                artifactStatus: result.artifactStatus,
                closeoutEvidenceStatus: result.closeoutEvidenceStatus,
                missingEvidence: result.missingEvidence,
                placeholderResidue: result.placeholderResidue,
                alignmentIssues: result.alignmentIssues,
                reviewNotes: result.reviewNotes,
                blockingIssues: result.blockingIssues,
                warnings: result.warnings,
                metadataHygieneFollowUps: result.metadataHygieneFollowUps,
                recoveryGuidance: result.recoveryGuidance,
                reviewHandoff: result.reviewHandoff,
                reviewReceipt: result.reviewReceipt,
                taskCloseActions: result.taskCloseActions,
                reviewResultPath: reviewResultPath ?? "implementation-summary-review.json",
                reviewResultPersistence: manifestReviewResultPath === null
                    ? "caller-managed"
                    : "manifest-owned"
            },
            findings: [...result.findings]
        });
    });
    return command;
}
