"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMetadataSyncStatusFromReviewCommand = createMetadataSyncStatusFromReviewCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const metadata_sync_and_inventory_1 = require("../../../lib/planning/metadata-sync-and-inventory");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
async function createMetadataSyncStatusFromReviewCommand() {
    const { Command } = await import("commander");
    const command = new Command("sync-status-from-review");
    command
        .description("Synchronize selected artifact status metadata from completed review evidence when the update is deterministic.")
        .option("--task-definition <path>", "Repository-relative path to the task-definition.md file to sync.")
        .option("--milestone-overview <path>", "Repository-relative path to the milestone-overview.md file to sync.")
        .option("--phase-overview <path>", "Repository-relative path to the phase-overview.md file to sync.")
        .option("--active-artifact <path>", "Repository-relative active artifact path expected by the current handoff.")
        .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 selectedOptions = [
            options.taskDefinition,
            options.milestoneOverview,
            options.phaseOverview
        ].filter((value) => typeof value === "string" && value.trim() !== "");
        if (selectedOptions.length !== 1) {
            throw (0, errors_1.createUsageError)("Choose exactly one metadata sync-status-from-review selector.", [
                "Use --task-definition, --milestone-overview, or --phase-overview.",
                "Review commands remain read-only; status sync requires this explicit write command."
            ]);
        }
        const commonOptions = {
            repositoryRoot: repositoryContext.root,
            activeArtifactPath: options.activeArtifact
        };
        const result = typeof options.taskDefinition === "string" && options.taskDefinition.trim() !== ""
            ? (0, metadata_sync_and_inventory_1.syncTaskDefinitionStatusFromReview)({
                ...commonOptions,
                rootPath: options.taskDefinition
            })
            : typeof options.milestoneOverview === "string" && options.milestoneOverview.trim() !== ""
                ? (0, metadata_sync_and_inventory_1.syncMilestoneOverviewStatusFromReview)({
                    ...commonOptions,
                    rootPath: options.milestoneOverview
                })
                : (0, metadata_sync_and_inventory_1.syncPhaseOverviewStatusFromReview)({
                    ...commonOptions,
                    rootPath: options.phaseOverview
                });
        (0, output_1.emitSuccess)({
            command: "metadata sync-status-from-review",
            format,
            text: (0, metadata_sync_and_inventory_1.renderMetadataSyncResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                artifactPath: result.artifactPath,
                artifactType: result.artifactType,
                taskDefinitionPath: result.taskDefinitionPath,
                pairedImplementationSummaryPath: result.pairedImplementationSummaryPath,
                milestoneReconciliationPath: result.milestoneReconciliationPath,
                source: result.source,
                changed: result.changed,
                writeApplied: result.writeApplied
            }
        });
    });
    return command;
}
