/**
 * @deprecated [DC-1] 无运行时消费方。类型被其他模块导入，运行时函数标记废弃。
 *
 * 交付就绪报告 — 问题十二 (Delivery Readiness Gate)。
 *
 * 核心规则:
 * - DeliveryReadinessReport 只判断 readiness，不隐式执行 commit/push/PR/deploy
 * - delivery_allowed=true 不等于 delivery executed
 * - 未验证不得 delivered
 * - blocking human confirmation 存在时 delivery_allowed 必须为 false
 */
import { evaluateMetrics } from "../contracts/metric_governance.js";
import { validateFinalAccess } from "../contracts/local_docker_acceptance.js";
/**
 * 评估交付就绪状态。
 * 只做判断，不执行任何交付动作。
 */
export function evaluateDeliveryReadiness(taskId, verificationResult, guardFindings, scopeCheckPassed, leaseCheckPassed, privacyCheckPassed, artifactAcceptancePassed, metricsData, acceptanceConfig) {
    const blockingFindings = [];
    const warnings = [];
    // 1. 验证结果检查
    if (!verificationResult) {
        blockingFindings.push({
            rule: "verification_required",
            message_zh: "无验证结果，不得交付",
        });
    }
    else if (verificationResult.status !== "passed") {
        blockingFindings.push({
            rule: "verification_passed_required",
            message_zh: `验证状态为 ${verificationResult.status}，不是 passed，不得交付`,
        });
    }
    else if (verificationResult.provenance !== "record_verification_execution") {
        blockingFindings.push({
            rule: "verification_provenance_required",
            message_zh: `验证结果 provenance 为 "${verificationResult.provenance}"，必须通过 recordVerificationExecution 录入，不得交付`,
        });
    }
    else if (!verificationResult.evidence_ids || verificationResult.evidence_ids.length === 0) {
        blockingFindings.push({
            rule: "verification_evidence_required",
            message_zh: "验证结果缺少 evidence_ids，不得交付",
        });
    }
    else if (!verificationResult.total_commands || verificationResult.total_commands === 0) {
        blockingFindings.push({
            rule: "verification_commands_required",
            message_zh: "验证结果 total_commands 为 0，不得交付",
        });
    }
    else if (verificationResult.missing_commands && verificationResult.missing_commands.length > 0) {
        blockingFindings.push({
            rule: "verification_plan_coverage_required",
            message_zh: `plan 命令未全覆盖，缺少: ${verificationResult.missing_commands.join(", ")}，不得交付`,
        });
    }
    else if (verificationResult.evidence_integrity_passed === false) {
        blockingFindings.push({
            rule: "verification_evidence_integrity_required",
            message_zh: "证据格式校验未全部通过，不得交付",
        });
    }
    // 2. Scope 检查
    if (!scopeCheckPassed) {
        blockingFindings.push({
            rule: "scope_check",
            message_zh: "范围检查未通过",
        });
    }
    // 3. Lease 检查
    if (!leaseCheckPassed) {
        blockingFindings.push({
            rule: "lease_check",
            message_zh: "存在活跃 lease 冲突，不得交付",
        });
    }
    // 4. 隐私检查
    if (!privacyCheckPassed) {
        blockingFindings.push({
            rule: "privacy_check",
            message_zh: "隐私检查未通过，存在未脱敏内容",
        });
    }
    // 5. Artifact 检查
    if (!artifactAcceptancePassed) {
        blockingFindings.push({
            rule: "artifact_acceptance",
            message_zh: "产物验收未通过",
        });
    }
    // 6. Guard 阻断检查
    const blockedGuards = guardFindings.filter((f) => f.blocked);
    for (const g of blockedGuards) {
        blockingFindings.push({
            rule: g.guard_id,
            message_zh: g.reason_zh,
        });
    }
    // 7. 守卫警告
    const warningGuards = guardFindings.filter((f) => !f.blocked && f.severity === "warning");
    for (const g of warningGuards) {
        warnings.push({
            rule: g.guard_id,
            message_zh: g.reason_zh,
        });
    }
    // 8. 指标治理门控 (problem-19: evaluateMetrics) — 仅当提供真实指标数据时检查
    if (metricsData && metricsData.length > 0) {
        const metricResult = evaluateMetrics(metricsData);
        if (!metricResult.passed) {
            blockingFindings.push({
                rule: "metric_governance",
                message_zh: `指标治理检查: ${metricResult.falsified_count} 个指标未通过`,
            });
        }
    }
    // 9. 最终验收门控 (problem-52: validateFinalAccess) — 仅当提供真实验收配置时检查
    if (acceptanceConfig) {
        const finalAccessResult = validateFinalAccess(acceptanceConfig.final_access, acceptanceConfig.project_run_mode);
        if (!finalAccessResult.passed) {
            blockingFindings.push({
                rule: "local_acceptance_final_access",
                message_zh: `最终验收检查: ${finalAccessResult.blocking_reasons.join(", ")}`,
            });
        }
    }
    // 判断就绪状态
    let status;
    const deliveryAllowed = blockingFindings.length === 0;
    if (blockedGuards.some((g) => g.action === "require_human")) {
        status = "manual_required";
    }
    else if (!deliveryAllowed) {
        status = "blocked";
    }
    else if (warnings.length > 0) {
        status = "ready";
    }
    else {
        status = "ready";
    }
    return {
        report_id: `dr-${taskId}-${Date.now()}`,
        task_id: taskId,
        delivery_readiness_status: status,
        delivery_allowed: deliveryAllowed,
        verification_result_id: verificationResult?.result_id,
        scope_check_passed: scopeCheckPassed,
        lease_check_passed: leaseCheckPassed,
        privacy_check_passed: privacyCheckPassed,
        artifact_acceptance_passed: artifactAcceptancePassed,
        blocking_findings: blockingFindings,
        warnings,
        evaluated_at: new Date().toISOString(),
        delivery_executed: false, // 就绪报告不触发执行
    };
}
/**
 * 创建交付执行记录。
 * 必须由 sf_deliver 或手动触发，不能由 readiness 自动触发。
 */
export function createDeliveryExecutionRecord(taskId, trigger, details) {
    return {
        execution_id: `de-${taskId}-${Date.now()}`,
        task_id: taskId,
        trigger,
        commit_sha: details.commit_sha,
        pr_url: details.pr_url,
        changelog_entry: details.changelog_entry,
        executed_at: new Date().toISOString(),
        rolled_back: false,
    };
}
/**
 * 验证: delivery_readiness 不得触发 delivery_execution。
 */
export function validateDeliverySeparation(report, executionRecord) {
    const errors = [];
    if (report.delivery_allowed && report.delivery_executed) {
        errors.push({
            error: "delivery_readiness 报告不应触发 delivery_execution",
            severity: "hard_fail",
        });
    }
    if (executionRecord && !report.delivery_allowed) {
        errors.push({
            error: "交付已执行但 delivery_readiness 未允许",
            severity: "hard_fail",
        });
    }
    if (executionRecord && executionRecord.trigger === undefined) {
        errors.push({
            error: "交付执行记录缺少显式触发来源",
            severity: "hard_fail",
        });
    }
    return errors;
}
//# sourceMappingURL=delivery_readiness.js.map