"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTaskReadinessReviewCommand = createTaskReadinessReviewCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const global_format_1 = require("../global-format");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const task_readiness_review_1 = require("../../../lib/planning/task-readiness-review");
const review_output_contracts_1 = require("../../../lib/planning/review-output-contracts");
async function createTaskReadinessReviewCommand() {
    const { Command } = await import("commander");
    const command = new Command("readiness-review");
    command
        .description("Evaluate task readiness as a read-only, no-write evidence surface and report missing evidence, dependency blockers, and sequencing issues.")
        .requiredOption("--task-definition <path>", "Repository-relative path to the task-definition.md file to review.")
        .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 result = (0, task_readiness_review_1.reviewTaskReadiness)(repositoryContext.root, {
            taskDefinitionPath: options.taskDefinition
        });
        (0, output_1.emitSuccess)({
            command: "task readiness-review",
            format,
            text: renderTaskReadinessReviewText(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                readOnly: true,
                writeCapable: false,
                noWriteBoundary: (0, review_output_contracts_1.getReviewOutputContract)("task readiness-review").noWriteBoundary,
                reviewOutputContract: (0, review_output_contracts_1.getReviewOutputContract)("task readiness-review"),
                taskDefinitionPath: result.taskDefinitionPath,
                reviewedReadiness: result.reviewedReadiness,
                readinessStatus: result.readinessStatus,
                handoffStatus: result.handoffStatus,
                pairedImplementationSummaryPath: result.pairedImplementationSummaryPath,
                milestoneReconciliationPath: result.milestoneReconciliationPath,
                reviewBoundaryKind: result.reviewBoundaryKind,
                missingEvidence: result.missingEvidence,
                dependencyBlockers: result.dependencyBlockers,
                sequencingIssues: result.sequencingIssues,
                handoffBlockers: result.handoffBlockers,
                reviewBoundaryWarnings: result.reviewBoundaryWarnings,
                blockingIssues: result.blockingIssues,
                warnings: result.warnings,
                recoveryGuidance: result.recoveryGuidance,
                executionProfileReadiness: result.executionProfileReadiness
            },
            findings: [...result.findings]
        });
    });
    return command;
}
function renderTaskReadinessReviewText(result) {
    const lines = [
        (0, output_1.renderTerminalHeading)("Task readiness review", "title").trimEnd(),
        "",
        (0, output_1.renderTerminalSection)({
            heading: "Summary",
            fields: [
                { label: "Task definition", value: result.taskDefinitionPath },
                { label: "No-write review surface", value: "yes" },
                { label: "Reviewed readiness", value: result.reviewedReadiness ?? "(missing)" },
                { label: "Readiness status", value: result.readinessStatus }
            ],
            lines: [(0, output_1.renderTerminalBoundary)({ kind: "read-only" })]
        }),
        "",
        (0, output_1.renderTerminalSection)({
            heading: "Review Output Contract",
            lines: (0, review_output_contracts_1.renderReviewOutputContractLines)()
        }),
        "",
        "Missing Evidence:"
    ];
    appendIssueLines(lines, result.missingEvidence);
    lines.push("", "Dependency Blockers:");
    appendIssueLines(lines, result.dependencyBlockers);
    lines.push("", "Sequencing Issues:");
    appendIssueLines(lines, result.sequencingIssues);
    lines.push("", `Paired Implementation Summary: ${result.pairedImplementationSummaryPath}`, `Implementation Handoff Status: ${result.handoffStatus}`, "Handoff Blockers:");
    appendIssueLines(lines, result.handoffBlockers);
    lines.push("");
    if (result.reviewBoundaryKind === "lightweight-task-summary") {
        lines.push(`Review Boundary: lightweight task summary (${result.pairedImplementationSummaryPath})`);
    }
    else {
        lines.push(`Milestone Reconciliation Path: ${result.milestoneReconciliationPath}`);
    }
    lines.push("Review Boundary Warnings:");
    appendIssueLines(lines, result.reviewBoundaryWarnings);
    lines.push("", "Recovery Guidance:");
    appendIssueLines(lines, result.recoveryGuidance);
    if (result.findings.length > 0) {
        lines.push("", (0, output_1.renderValidationFindingsText)(result.findings, "Structured Findings"));
    }
    return lines.join("\n");
}
function appendIssueLines(lines, issues) {
    if (issues.length === 0) {
        lines.push("- (none)");
        return;
    }
    for (const issue of issues) {
        lines.push(`- ${issue}`);
    }
}
