"""候選審核 queue AI summary Telegram dispatch report catalog record final closeout gate。

本模組只在 catalog record archive summary 通過後整理終局收尾證據；不讀
approval 或 Telegram token、不呼叫 LLM、不派送 Telegram、不開 DB、不寫檔、
不執行 CLI、不補寫 catalog record、不執行 commit、不更新 review_state、
不掛 scheduler。
"""

from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary import (
    FALSE_RESPONSE_KEYS as REPORT_CATALOG_RECORD_ARCHIVE_SUMMARY_FALSE_RESPONSE_KEYS,
    SAFE_APPROVAL_ENV_VAR,
    SAFE_TOKEN_METADATA_KEYS,
    TARGET_COLUMN,
    TARGET_TABLE,
    _as_dict,
    _as_list,
    _contains_forbidden_token_key,
    _has_text,
    _safe_int,
    _safe_text,
    _strip_safe_token_boolean_keys,
)


REQUIRED_ARCHIVE_SUMMARY_BOUNDARIES = {
    "do_not_read_approval_token_from_report_catalog_record_archive_summary_api",
    "do_not_read_telegram_token_from_report_catalog_record_archive_summary_api",
    "do_not_call_llm_from_report_catalog_record_archive_summary",
    "do_not_generate_report_from_report_catalog_record_archive_summary_api",
    "do_not_write_report_catalog_record_archive_summary_artifact_from_api",
    "do_not_execute_catalog_record_cli_from_report_catalog_record_archive_summary_api",
    "do_not_write_report_catalog_record_from_report_catalog_record_archive_summary_api",
    "do_not_commit_catalog_record_from_report_catalog_record_archive_summary_api",
    "do_not_dispatch_telegram_from_report_catalog_record_archive_summary_api",
    "do_not_open_database_connection_from_report_catalog_record_archive_summary",
    "do_not_update_review_state_from_report_catalog_record_archive_summary",
    "do_not_attach_scheduler_from_report_catalog_record_archive_summary",
    "future_market_intel_report_catalog_record_final_closeout_must_use_separate_gate",
}
FALSE_RESPONSE_KEYS = tuple(
    dict.fromkeys(
        REPORT_CATALOG_RECORD_ARCHIVE_SUMMARY_FALSE_RESPONSE_KEYS
        + (
            "telegram_dispatch_report_catalog_record_final_closeout_gate_file_written",
            "report_catalog_record_final_closeout_gate_file_written",
            "catalog_record_final_closeout_gate_file_written",
            "telegram_dispatch_report_catalog_record_final_closeout_file_written",
            "report_catalog_record_final_closeout_file_written",
            "catalog_record_final_closeout_file_written",
            "catalog_record_final_closeout_record_written",
            "catalog_record_final_closeout_executed",
            "catalog_record_pipeline_completion_record_written",
            "market_intel_catalog_record_pipeline_completed_by_api",
        )
    )
)


def _archive_summary_snapshot(telegram_dispatch_report_catalog_record_archive_summary):
    summary = _as_dict(telegram_dispatch_report_catalog_record_archive_summary)
    archive = _as_dict(summary.get("telegram_dispatch_report_catalog_record_archive"))
    operator = _as_dict(
        summary.get("operator_telegram_dispatch_report_catalog_record_archive_summary")
    )
    promotion = _as_dict(summary.get("promotion_gate"))
    sections = _as_list(
        summary.get("telegram_dispatch_report_catalog_record_archive_summary_sections")
    )
    section_keys = {
        str(section.get("key"))
        for section in sections
        if isinstance(section, dict) and section.get("key")
    }
    safe_boundaries = set(str(item) for item in _as_list(summary.get("safe_boundaries")))
    missing_boundaries = sorted(REQUIRED_ARCHIVE_SUMMARY_BOUNDARIES - safe_boundaries)
    blocked_keys = [key for key in FALSE_RESPONSE_KEYS if bool(summary.get(key))]

    return {
        "provided": bool(summary),
        "mode": summary.get("mode"),
        "target_operation": summary.get("target_operation"),
        "archive_summary_passed": bool(
            summary.get("telegram_dispatch_report_catalog_record_archive_summary_passed")
            or summary.get(
                "summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_passed"
            )
            or summary.get("report_catalog_record_archive_summary_passed")
        ),
        "ready_for_next_manual_phase": bool(summary.get("ready_for_next_manual_phase")),
        "ready_for_market_intel_report_catalog_record_final_closeout": bool(
            summary.get("ready_for_market_intel_report_catalog_record_final_closeout")
        ),
        "ready_for_market_intel_report_catalog_record_archive_summary": bool(
            summary.get("ready_for_market_intel_report_catalog_record_archive_summary")
        ),
        "statement_count": _safe_int(summary.get("statement_count")),
        "expected_summary_payload_hash": _safe_text(
            summary.get("expected_summary_payload_hash")
            or archive.get("expected_summary_payload_hash"),
            80,
        ),
        "writer_record_family": _safe_text(archive.get("writer_record_family"), 80),
        "writer_catalog_record_key": _safe_text(
            archive.get("writer_catalog_record_key"), 160
        ),
        "writer_target_table": archive.get("writer_target_table"),
        "writer_target_column": archive.get("writer_target_column"),
        "writer_statement_count": _safe_int(archive.get("writer_statement_count")),
        "writer_database_commit_executed": bool(
            archive.get("writer_database_commit_executed")
        ),
        "writer_hash_match": bool(archive.get("writer_hash_match")),
        "smoke_read_only_query_executed": bool(
            archive.get("smoke_read_only_query_executed")
        ),
        "smoke_postwrite_smoke_passed": bool(
            archive.get("smoke_postwrite_smoke_passed")
        ),
        "smoke_hash_match": bool(archive.get("smoke_hash_match")),
        "archive_passed": bool(archive.get("archive_passed")),
        "archive_artifact_path": archive.get("archive_artifact_path"),
        "archive_closeout_artifact_path_recorded": bool(
            archive.get("archive_closeout_artifact_path_recorded")
        ),
        "archive_commit_artifact_path_recorded": bool(
            archive.get("archive_commit_artifact_path_recorded")
        ),
        "archive_run_receipt_artifact_path_recorded": bool(
            archive.get("archive_run_receipt_artifact_path_recorded")
        ),
        "archive_writer_output_artifact_path_recorded": bool(
            archive.get("archive_writer_output_artifact_path_recorded")
        ),
        "archive_postwrite_smoke_artifact_path_recorded": bool(
            archive.get("archive_postwrite_smoke_artifact_path_recorded")
        ),
        "archive_backup_artifact_path_recorded": bool(
            archive.get("archive_backup_artifact_path_recorded")
        ),
        "operator_summary_artifact_path": operator.get(
            "report_catalog_record_archive_summary_artifact_path"
        ),
        "operator_archive_artifact_path_recorded": bool(
            operator.get("report_catalog_record_archive_artifact_path_recorded")
        ),
        "operator_archive_summary_gate": bool(
            operator.get("operator_confirmed_report_catalog_record_archive_summary_gate")
        ),
        "operator_archive_reviewed": bool(
            operator.get("operator_confirmed_report_catalog_record_archive_reviewed")
        ),
        "operator_traceability_reviewed": bool(
            operator.get("operator_confirmed_catalog_record_traceability_reviewed")
        ),
        "operator_archive_summary_read_only": bool(
            operator.get("operator_confirmed_catalog_record_archive_summary_read_only")
        ),
        "operator_final_closeout_requires_separate_gate": bool(
            operator.get(
                "operator_confirmed_catalog_record_final_closeout_requires_separate_gate"
            )
        ),
        "section_keys": sorted(section_keys),
        "summary_sections_complete": {
            "catalog_record_archive_summary_identity",
            "catalog_record_archive_summary_traceability",
            "catalog_record_archive_summary_integrity",
            "catalog_record_archive_summary_safety",
        }.issubset(section_keys),
        "promotion_allowed": bool(promotion.get("allowed")),
        "promotion_next_manual_phase": promotion.get("next_manual_phase"),
        "promotion_final_closeout_separate_gate": bool(
            promotion.get("report_catalog_record_final_closeout_requires_separate_gate")
        ),
        "promotion_api_must_not_generate_report": bool(
            promotion.get("api_must_not_generate_report")
        ),
        "promotion_api_must_not_write_file": bool(
            promotion.get("api_must_not_write_file")
        ),
        "promotion_api_must_not_execute_cli": bool(
            promotion.get("api_must_not_execute_cli")
        ),
        "promotion_api_must_not_write_catalog_record": bool(
            promotion.get("api_must_not_write_catalog_record")
        ),
        "promotion_api_must_not_write_database": bool(
            promotion.get("api_must_not_write_database")
        ),
        "promotion_api_must_not_commit_catalog_record": bool(
            promotion.get("api_must_not_commit_catalog_record")
        ),
        "promotion_api_must_not_dispatch_telegram": bool(
            promotion.get("api_must_not_dispatch_telegram")
        ),
        "safe_boundaries_complete": not missing_boundaries,
        "missing_safe_boundaries": missing_boundaries,
        "forbidden_token_key_detected": _contains_forbidden_token_key(
            _strip_safe_token_boolean_keys(summary)
        ),
        "blocked_response_keys_true": blocked_keys,
        **{key: bool(summary.get(key)) for key in FALSE_RESPONSE_KEYS},
    }


def _operator_summary(operator_evidence, report_catalog_record_final_closeout=None):
    operator_evidence = _as_dict(operator_evidence)
    closeout_payload = _as_dict(report_catalog_record_final_closeout)
    return {
        "provided_keys": sorted(operator_evidence.keys()),
        "report_catalog_record_final_closeout_artifact_path": _safe_text(
            operator_evidence.get("report_catalog_record_final_closeout_artifact_path")
            or operator_evidence.get(
                "telegram_dispatch_report_catalog_record_final_closeout_artifact_path"
            )
            or operator_evidence.get(
                "market_intel_report_catalog_record_final_closeout_artifact_path"
            )
            or closeout_payload.get("report_catalog_record_final_closeout_artifact_path"),
            300,
        ),
        "report_catalog_record_archive_summary_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_archive_summary_artifact_path")
            or closeout_payload.get("report_catalog_record_archive_summary_artifact_path")
        ),
        "report_catalog_record_archive_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_archive_artifact_path")
            or closeout_payload.get("report_catalog_record_archive_artifact_path")
        ),
        "report_catalog_record_closeout_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_closeout_artifact_path")
            or closeout_payload.get("report_catalog_record_closeout_artifact_path")
        ),
        "report_catalog_record_commit_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_commit_artifact_path")
            or closeout_payload.get("report_catalog_record_commit_artifact_path")
        ),
        "report_catalog_record_run_receipt_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_run_receipt_artifact_path")
            or closeout_payload.get("report_catalog_record_run_receipt_artifact_path")
        ),
        "catalog_record_writer_output_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_writer_output_artifact_path")
            or operator_evidence.get("writer_output_json_path")
            or closeout_payload.get("catalog_record_writer_output_artifact_path")
        ),
        "catalog_record_postwrite_smoke_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_postwrite_smoke_artifact_path")
            or operator_evidence.get("postwrite_smoke_json_path")
            or closeout_payload.get("catalog_record_postwrite_smoke_artifact_path")
        ),
        "catalog_record_backup_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_backup_artifact_path")
            or operator_evidence.get("backup_artifact_path")
            or closeout_payload.get("catalog_record_backup_artifact_path")
        ),
        "operator_confirmed_report_catalog_record_final_closeout_gate": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_final_closeout_gate"
            )
            or operator_evidence.get(
                "operator_confirmed_market_intel_report_catalog_record_final_closeout"
            )
            or closeout_payload.get(
                "operator_confirmed_report_catalog_record_final_closeout_gate"
            )
        ),
        "operator_confirmed_report_catalog_record_archive_summary_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_archive_summary_reviewed"
            )
            or operator_evidence.get(
                "operator_confirmed_catalog_record_archive_summary_reviewed"
            )
            or closeout_payload.get(
                "operator_confirmed_report_catalog_record_archive_summary_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_traceability_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_traceability_reviewed"
            )
            or closeout_payload.get("operator_confirmed_catalog_record_traceability_reviewed")
        ),
        "operator_confirmed_catalog_record_pipeline_complete": bool(
            operator_evidence.get("operator_confirmed_catalog_record_pipeline_complete")
            or operator_evidence.get(
                "operator_confirmed_market_intel_catalog_record_pipeline_complete"
            )
            or closeout_payload.get("operator_confirmed_catalog_record_pipeline_complete")
        ),
        "operator_confirmed_no_pending_catalog_record_followup": bool(
            operator_evidence.get(
                "operator_confirmed_no_pending_catalog_record_followup"
            )
            or operator_evidence.get("operator_confirmed_no_followup_required")
            or closeout_payload.get(
                "operator_confirmed_no_pending_catalog_record_followup"
            )
        ),
        "operator_confirmed_catalog_record_final_closeout_read_only": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_final_closeout_read_only"
            )
            or operator_evidence.get("operator_confirmed_final_closeout_is_read_only")
            or closeout_payload.get(
                "operator_confirmed_catalog_record_final_closeout_read_only"
            )
        ),
        "operator_confirmed_no_token_in_report_catalog_record_final_closeout": bool(
            operator_evidence.get(
                "operator_confirmed_no_token_in_report_catalog_record_final_closeout"
            )
            or operator_evidence.get("operator_confirmed_no_token_in_artifacts")
            or closeout_payload.get(
                "operator_confirmed_no_token_in_report_catalog_record_final_closeout"
            )
        ),
        "operator_confirmed_no_api_report_generation": bool(
            operator_evidence.get("operator_confirmed_no_api_report_generation")
            or operator_evidence.get("operator_confirmed_no_api_report_write")
            or closeout_payload.get("operator_confirmed_no_api_report_generation")
        ),
        "operator_confirmed_no_api_file_write": bool(
            operator_evidence.get("operator_confirmed_no_api_file_write")
            or closeout_payload.get("operator_confirmed_no_api_file_write")
        ),
        "operator_confirmed_no_api_cli_execution": bool(
            operator_evidence.get("operator_confirmed_no_api_cli_execution")
            or operator_evidence.get("operator_confirmed_no_api_execute_cli")
            or closeout_payload.get("operator_confirmed_no_api_cli_execution")
        ),
        "operator_confirmed_no_api_catalog_record_write": bool(
            operator_evidence.get("operator_confirmed_no_api_catalog_record_write")
            or operator_evidence.get("operator_confirmed_no_api_db_write")
            or closeout_payload.get("operator_confirmed_no_api_catalog_record_write")
        ),
        "operator_confirmed_no_api_telegram_dispatch": bool(
            operator_evidence.get("operator_confirmed_no_api_telegram_dispatch")
            or closeout_payload.get("operator_confirmed_no_api_telegram_dispatch")
        ),
        "operator_confirmed_no_api_db_write": bool(
            operator_evidence.get("operator_confirmed_no_api_db_write")
            or closeout_payload.get("operator_confirmed_no_api_db_write")
        ),
        "operator_confirmed_no_llm_call": bool(
            operator_evidence.get("operator_confirmed_no_llm_call")
            or closeout_payload.get("operator_confirmed_no_llm_call")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            operator_evidence.get("operator_confirmed_no_scheduler_attach")
            or closeout_payload.get("operator_confirmed_no_scheduler_attach")
        ),
        "final_closeout_notes_recorded": _has_text(
            operator_evidence.get("report_catalog_record_final_closeout_notes")
            or operator_evidence.get("catalog_record_final_closeout_notes")
            or closeout_payload.get("notes")
        ),
        "safe_token_metadata_only": all(
            key in SAFE_TOKEN_METADATA_KEYS or key == "approval_env_var"
            for key in _strip_safe_token_boolean_keys(operator_evidence)
            if "token" in str(key).lower() or str(key).lower() == "approval_env_var"
        )
        and operator_evidence.get("approval_env_var", SAFE_APPROVAL_ENV_VAR)
        == SAFE_APPROVAL_ENV_VAR,
        "forbidden_token_submitted_to_api": _contains_forbidden_token_key(
            operator_evidence
        )
        or _contains_forbidden_token_key(closeout_payload),
    }


def _closeout_sections(summary, operator):
    return [
        {
            "key": "catalog_record_final_closeout_identity",
            "title": "Catalog record final closeout identity",
            "facts": [
                f"family={summary['writer_record_family'] or 'missing'}",
                f"record_key={summary['writer_catalog_record_key'] or 'missing'}",
                f"statement_count={summary['writer_statement_count']}",
            ],
        },
        {
            "key": "catalog_record_final_closeout_traceability",
            "title": "Catalog record final closeout traceability",
            "facts": [
                f"summary_path={operator['report_catalog_record_final_closeout_artifact_path'] or 'missing'}",
                f"archive_summary_passed={summary['archive_summary_passed']}",
                f"sections_complete={summary['summary_sections_complete']}",
            ],
        },
        {
            "key": "catalog_record_final_closeout_integrity",
            "title": "Catalog record final closeout integrity",
            "facts": [
                f"db_commit={summary['writer_database_commit_executed']}",
                f"smoke_passed={summary['smoke_postwrite_smoke_passed']}",
                f"pipeline_complete={operator['operator_confirmed_catalog_record_pipeline_complete']}",
            ],
        },
        {
            "key": "catalog_record_final_closeout_safety",
            "title": "Catalog record final closeout safety",
            "facts": [
                "report_generated=false",
                "api_cli_executed=false",
                "api_database_write_executed=false",
                "catalog_record_final_closeout_executed=false",
                "scheduler_attached=false",
            ],
        },
    ]


def _closeout_gates(summary, operator, apply_real_write):
    return [
        {
            "key": "report_catalog_record_archive_summary_passed",
            "label": "上一關 report catalog record archive summary gate 已通過",
            "passed": bool(
                summary["provided"]
                and summary["mode"]
                == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview"
                and summary["archive_summary_passed"]
                and summary["ready_for_next_manual_phase"]
                and summary["ready_for_market_intel_report_catalog_record_final_closeout"]
                and not summary["ready_for_market_intel_report_catalog_record_archive_summary"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_catalog_record_evidence_retained",
            "label": "archive summary payload 保留 catalog record identity、DB commit 與 post-write smoke 證據",
            "passed": bool(
                summary["writer_catalog_record_key"]
                and summary["writer_target_table"] == TARGET_TABLE
                and summary["writer_target_column"] == TARGET_COLUMN
                and summary["writer_statement_count"] > 0
                and summary["writer_database_commit_executed"]
                and summary["writer_hash_match"]
                and summary["smoke_read_only_query_executed"]
                and summary["smoke_postwrite_smoke_passed"]
                and summary["smoke_hash_match"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_artifacts_verified",
            "label": "archive summary payload 保留 archive、closeout、commit、run receipt、writer output、post-write smoke 與 backup trace",
            "passed": bool(
                summary["archive_passed"]
                and summary["archive_artifact_path"]
                and summary["archive_closeout_artifact_path_recorded"]
                and summary["archive_commit_artifact_path_recorded"]
                and summary["archive_run_receipt_artifact_path_recorded"]
                and summary["archive_writer_output_artifact_path_recorded"]
                and summary["archive_postwrite_smoke_artifact_path_recorded"]
                and summary["archive_backup_artifact_path_recorded"]
                and summary["operator_summary_artifact_path"]
                and summary["operator_archive_artifact_path_recorded"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_sections_complete",
            "label": "archive summary sections 已整理 identity、traceability、integrity 與 runtime safety",
            "passed": bool(summary["summary_sections_complete"]),
        },
        {
            "key": "report_catalog_record_archive_summary_operator_evidence_retained",
            "label": "archive summary payload 保留操作員 archive summary、traceability、read-only 與 final closeout separate gate 確認",
            "passed": bool(
                summary["operator_archive_summary_gate"]
                and summary["operator_archive_reviewed"]
                and summary["operator_traceability_reviewed"]
                and summary["operator_archive_summary_read_only"]
                and summary["operator_final_closeout_requires_separate_gate"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_promotion_gate",
            "label": "archive summary promotion gate 僅放行到 final closeout 且要求獨立 gate",
            "passed": bool(
                summary["promotion_allowed"]
                and summary["promotion_next_manual_phase"]
                == "market_intel_report_catalog_record_final_closeout"
                and summary["promotion_final_closeout_separate_gate"]
                and summary["promotion_api_must_not_generate_report"]
                and summary["promotion_api_must_not_write_file"]
                and summary["promotion_api_must_not_execute_cli"]
                and summary["promotion_api_must_not_write_catalog_record"]
                and summary["promotion_api_must_not_write_database"]
                and summary["promotion_api_must_not_commit_catalog_record"]
                and summary["promotion_api_must_not_dispatch_telegram"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_api_side_effects_blocked",
            "label": "上一關 archive summary top-level API 沒有執行 CLI、寫檔、寫 DB、commit、派 Telegram 或掛 scheduler",
            "passed": bool(
                not summary["api_executes_cli"]
                and not summary["api_writes_file"]
                and not summary["api_writes_database"]
                and not summary["api_dispatches_telegram"]
                and not summary["database_write_executed"]
                and not summary["catalog_record_archive_summary_executed"]
                and not summary["scheduler_attached"]
                and not summary["blocked_response_keys_true"]
            ),
        },
        {
            "key": "report_catalog_record_archive_summary_safe_boundaries_complete",
            "label": "上一關 archive summary safe boundaries 完整且未夾帶 token key",
            "passed": bool(
                summary["safe_boundaries_complete"]
                and not summary["forbidden_token_key_detected"]
            ),
        },
        {
            "key": "report_catalog_record_final_closeout_operator_confirmed",
            "label": "操作員確認 final closeout gate、完整 traceability、pipeline complete 與無後續 DB follow-up",
            "passed": bool(
                operator["report_catalog_record_final_closeout_artifact_path"]
                and operator[
                    "report_catalog_record_archive_summary_artifact_path_recorded"
                ]
                and operator["report_catalog_record_archive_artifact_path_recorded"]
                and operator["report_catalog_record_closeout_artifact_path_recorded"]
                and operator["report_catalog_record_commit_artifact_path_recorded"]
                and operator["report_catalog_record_run_receipt_artifact_path_recorded"]
                and operator["catalog_record_writer_output_artifact_path_recorded"]
                and operator["catalog_record_postwrite_smoke_artifact_path_recorded"]
                and operator["catalog_record_backup_artifact_path_recorded"]
                and operator[
                    "operator_confirmed_report_catalog_record_final_closeout_gate"
                ]
                and operator[
                    "operator_confirmed_report_catalog_record_archive_summary_reviewed"
                ]
                and operator["operator_confirmed_catalog_record_traceability_reviewed"]
                and operator["operator_confirmed_catalog_record_pipeline_complete"]
                and operator[
                    "operator_confirmed_no_pending_catalog_record_followup"
                ]
                and operator[
                    "operator_confirmed_catalog_record_final_closeout_read_only"
                ]
            ),
        },
        {
            "key": "report_catalog_record_final_closeout_runtime_boundaries_confirmed",
            "label": "操作員確認本 API 不產報表、不寫檔、不執行 CLI、不寫 DB、不派 Telegram、不呼叫 LLM、不掛 scheduler",
            "passed": bool(
                operator[
                    "operator_confirmed_no_token_in_report_catalog_record_final_closeout"
                ]
                and operator["operator_confirmed_no_api_report_generation"]
                and operator["operator_confirmed_no_api_file_write"]
                and operator["operator_confirmed_no_api_cli_execution"]
                and operator["operator_confirmed_no_api_catalog_record_write"]
                and operator["operator_confirmed_no_api_telegram_dispatch"]
                and operator["operator_confirmed_no_api_db_write"]
                and operator["operator_confirmed_no_llm_call"]
                and operator["operator_confirmed_no_scheduler_attach"]
            ),
        },
        {
            "key": "report_catalog_record_final_closeout_no_token_submitted_to_api",
            "label": "final closeout gate payload 不得包含 approval 或 Telegram token key",
            "passed": not operator["forbidden_token_submitted_to_api"],
        },
        {
            "key": "report_catalog_record_final_closeout_apply_real_write_not_requested_from_api",
            "label": "API/UI report catalog record final closeout gate 不接受 apply_real_write",
            "passed": not apply_real_write,
        },
    ]


def build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(
    *,
    telegram_dispatch_report_catalog_record_archive_summary,
    report_catalog_record_final_closeout=None,
    operator_evidence=None,
    execute_requested=False,
    apply_real_write=False,
):
    """建立 Telegram dispatch report catalog record final closeout gate；不執行副作用。"""
    summary = _archive_summary_snapshot(
        telegram_dispatch_report_catalog_record_archive_summary
    )
    operator = _operator_summary(operator_evidence, report_catalog_record_final_closeout)
    sections = _closeout_sections(summary, operator)
    gates = _closeout_gates(summary, operator, bool(apply_real_write))
    blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
    closeout_passed = bool(not blocked_reasons)

    return {
        "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview",
        "target_table": TARGET_TABLE,
        "target_column": TARGET_COLUMN,
        "target_json_path": [
            "market_intel_ai_summary",
            "telegram_dispatch_report_catalog_record_final_closeout",
        ],
        "target_operation": "close_manual_market_intel_report_catalog_record_pipeline",
        "execute_requested": bool(execute_requested),
        "apply_real_write_requested": bool(apply_real_write),
        "report_catalog_record_final_closeout_reviewed": True,
        "telegram_dispatch_report_catalog_record_final_closeout_passed": closeout_passed,
        "summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_passed": closeout_passed,
        "report_catalog_record_final_closeout_passed": closeout_passed,
        "market_intel_report_catalog_record_pipeline_complete": closeout_passed,
        "ready_for_next_manual_phase": False,
        "ready_for_market_intel_report_catalog_record_final_closeout": False,
        "ready_for_market_intel_report_catalog_record_archive_summary": False,
        "ready_for_market_intel_report_catalog_record_archive": False,
        "ready_for_market_intel_report_catalog_record_closeout": False,
        "ready_for_market_intel_report_catalog_record_commit": False,
        "ready_for_market_intel_report_catalog_record_cli_run": False,
        "ready_for_cli_operator_run": False,
        "ready_for_real_write": False,
        "ready_for_report_generation": False,
        "ready_for_telegram_dispatch": False,
        "ready_for_api_database_write": False,
        "ready_for_scheduler_attach": False,
        "ready_for_llm_call": False,
        "api_executes_cli": False,
        "api_executes_llm": False,
        "api_dispatches_telegram": False,
        "api_reads_approval_token": False,
        "api_writes_file": False,
        "api_writes_database": False,
        "api_updates_review_state": False,
        **{key: False for key in FALSE_RESPONSE_KEYS},
        "statement_count": summary["writer_statement_count"]
        or summary["statement_count"],
        "expected_summary_payload_hash": summary["expected_summary_payload_hash"],
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "telegram_dispatch_report_catalog_record_archive_summary": summary,
        "telegram_dispatch_report_catalog_record_final_closeout_sections": sections,
        "operator_telegram_dispatch_report_catalog_record_final_closeout": operator,
        "promotion_gate": {
            "allowed": closeout_passed,
            "next_manual_phase": "market_intel_report_catalog_record_pipeline_complete",
            "terminal_gate": True,
            "requires_real_db_write": False,
            "requires_cli_run": False,
            "requires_scheduler_attach": False,
            "requires_operator_approval": True,
            "requires_postwrite_smoke": False,
            "api_must_not_generate_report": True,
            "api_must_not_write_file": True,
            "api_must_not_execute_cli": True,
            "api_must_not_write_catalog_record": True,
            "api_must_not_write_database": True,
            "api_must_not_dispatch_telegram": True,
            "api_must_not_commit_catalog_record": True,
            "report_catalog_record_pipeline_complete_no_separate_api_gate": True,
        },
        "next_operator_steps": [
            "保存 report catalog record final closeout artifact path",
            "確認 catalog record pipeline 已完成且沒有待補 DB write、Telegram dispatch 或 report generation",
            "將後續工作轉為人工營運檢視，不從 API/UI 觸發任何寫入或派送",
            "若未來需要重開流程，必須從新的獨立 gate 與人工核准開始",
        ],
        "safe_boundaries": [
            "do_not_read_approval_token_from_report_catalog_record_final_closeout_api",
            "do_not_read_telegram_token_from_report_catalog_record_final_closeout_api",
            "do_not_call_llm_from_report_catalog_record_final_closeout",
            "do_not_generate_report_from_report_catalog_record_final_closeout_api",
            "do_not_write_report_catalog_record_final_closeout_artifact_from_api",
            "do_not_execute_catalog_record_cli_from_report_catalog_record_final_closeout_api",
            "do_not_write_report_catalog_record_from_report_catalog_record_final_closeout_api",
            "do_not_commit_catalog_record_from_report_catalog_record_final_closeout_api",
            "do_not_dispatch_telegram_from_report_catalog_record_final_closeout_api",
            "do_not_open_database_connection_from_report_catalog_record_final_closeout",
            "do_not_update_review_state_from_report_catalog_record_final_closeout",
            "do_not_attach_scheduler_from_report_catalog_record_final_closeout",
            "market_intel_report_catalog_record_pipeline_terminal_preview_only",
            "no_remove_orphans",
            "no_momo_db_lifecycle_change",
        ],
    }
