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

本模組只審核 catalog record closeout 後的封存證據；不讀 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_closeout import (
    FALSE_RESPONSE_KEYS as REPORT_CATALOG_RECORD_CLOSEOUT_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_CLOSEOUT_BOUNDARIES = {
    "do_not_read_approval_token_from_report_catalog_record_closeout_api",
    "do_not_read_telegram_token_from_report_catalog_record_closeout_api",
    "do_not_call_llm_from_report_catalog_record_closeout",
    "do_not_generate_report_from_report_catalog_record_closeout_api",
    "do_not_write_report_catalog_record_closeout_artifact_from_api",
    "do_not_execute_catalog_record_cli_from_report_catalog_record_closeout_api",
    "do_not_write_report_catalog_record_from_report_catalog_record_closeout_api",
    "do_not_commit_catalog_record_from_report_catalog_record_closeout_api",
    "do_not_dispatch_telegram_from_report_catalog_record_closeout_api",
    "do_not_open_database_connection_from_report_catalog_record_closeout",
    "do_not_update_review_state_from_report_catalog_record_closeout",
    "do_not_attach_scheduler_from_report_catalog_record_closeout",
    "future_market_intel_report_catalog_record_archive_must_use_separate_gate",
}
FALSE_RESPONSE_KEYS = tuple(
    dict.fromkeys(
        REPORT_CATALOG_RECORD_CLOSEOUT_FALSE_RESPONSE_KEYS
        + (
            "telegram_dispatch_report_catalog_record_archive_gate_file_written",
            "report_catalog_record_archive_gate_file_written",
            "catalog_record_archive_gate_file_written",
            "telegram_dispatch_report_catalog_record_archive_file_written",
            "report_catalog_record_archive_file_written",
            "catalog_record_archive_file_written",
            "catalog_record_archive_record_written",
            "catalog_record_archive_manifest_written",
            "catalog_record_archive_executed",
            "catalog_record_archive_summary_gate_file_written",
        )
    )
)


def _closeout_summary(telegram_dispatch_report_catalog_record_closeout):
    closeout = _as_dict(telegram_dispatch_report_catalog_record_closeout)
    commit = _as_dict(closeout.get("telegram_dispatch_report_catalog_record_commit"))
    operator = _as_dict(
        closeout.get("operator_telegram_dispatch_report_catalog_record_closeout")
    )
    promotion = _as_dict(closeout.get("promotion_gate"))
    safe_boundaries = set(str(item) for item in _as_list(closeout.get("safe_boundaries")))
    missing_boundaries = sorted(REQUIRED_CLOSEOUT_BOUNDARIES - safe_boundaries)
    blocked_keys = [key for key in FALSE_RESPONSE_KEYS if bool(closeout.get(key))]

    return {
        "provided": bool(closeout),
        "mode": closeout.get("mode"),
        "target_operation": closeout.get("target_operation"),
        "closeout_passed": bool(
            closeout.get("telegram_dispatch_report_catalog_record_closeout_passed")
            or closeout.get(
                "summary_persistence_telegram_dispatch_report_catalog_record_closeout_passed"
            )
            or closeout.get("report_catalog_record_closeout_passed")
        ),
        "ready_for_next_manual_phase": bool(closeout.get("ready_for_next_manual_phase")),
        "ready_for_market_intel_report_catalog_record_archive": bool(
            closeout.get("ready_for_market_intel_report_catalog_record_archive")
        ),
        "ready_for_market_intel_report_catalog_record_closeout": bool(
            closeout.get("ready_for_market_intel_report_catalog_record_closeout")
        ),
        "statement_count": _safe_int(closeout.get("statement_count")),
        "expected_summary_payload_hash": _safe_text(
            closeout.get("expected_summary_payload_hash"),
            80,
        ),
        "writer_record_family": _safe_text(commit.get("writer_record_family"), 80),
        "writer_catalog_record_key": _safe_text(
            commit.get("writer_catalog_record_key"), 160
        ),
        "writer_target_table": commit.get("writer_target_table"),
        "writer_target_column": commit.get("writer_target_column"),
        "writer_statement_count": _safe_int(commit.get("writer_statement_count")),
        "writer_database_commit_executed": bool(
            commit.get("writer_database_commit_executed")
        ),
        "writer_hash_match": bool(commit.get("writer_hash_match")),
        "smoke_read_only_query_executed": bool(
            commit.get("smoke_read_only_query_executed")
        ),
        "smoke_postwrite_smoke_passed": bool(
            commit.get("smoke_postwrite_smoke_passed")
        ),
        "smoke_hash_match": bool(commit.get("smoke_hash_match")),
        "operator_closeout_gate": bool(
            operator.get("operator_confirmed_report_catalog_record_closeout_gate")
        ),
        "operator_commit_reviewed": bool(
            operator.get("operator_confirmed_report_catalog_record_commit_reviewed")
        ),
        "operator_db_commit_observed": bool(
            operator.get("operator_confirmed_catalog_record_db_commit_observed")
        ),
        "operator_no_followup_db_write_required": bool(
            operator.get(
                "operator_confirmed_catalog_record_no_followup_db_write_required"
            )
        ),
        "operator_closeout_summary_ready": bool(
            operator.get("operator_confirmed_catalog_record_closeout_summary_ready")
        ),
        "operator_archive_requires_separate_gate": bool(
            operator.get("operator_confirmed_catalog_record_archive_requires_separate_gate")
        ),
        "operator_closeout_artifact_path": operator.get(
            "report_catalog_record_closeout_artifact_path"
        ),
        "operator_commit_artifact_path_recorded": bool(
            operator.get("report_catalog_record_commit_artifact_path_recorded")
        ),
        "operator_run_receipt_artifact_path_recorded": bool(
            operator.get("report_catalog_record_run_receipt_artifact_path_recorded")
        ),
        "operator_writer_output_artifact_path_recorded": bool(
            operator.get("catalog_record_writer_output_artifact_path_recorded")
        ),
        "operator_postwrite_smoke_artifact_path_recorded": bool(
            operator.get("catalog_record_postwrite_smoke_artifact_path_recorded")
        ),
        "operator_backup_artifact_path_recorded": bool(
            operator.get("catalog_record_backup_artifact_path_recorded")
        ),
        "promotion_allowed": bool(promotion.get("allowed")),
        "promotion_next_manual_phase": promotion.get("next_manual_phase"),
        "promotion_archive_separate_gate": bool(
            promotion.get("report_catalog_record_archive_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(closeout)
        ),
        "blocked_response_keys_true": blocked_keys,
        **{key: bool(closeout.get(key)) for key in FALSE_RESPONSE_KEYS},
    }


def _operator_summary(operator_evidence, report_catalog_record_archive=None):
    operator_evidence = _as_dict(operator_evidence)
    archive_payload = _as_dict(report_catalog_record_archive)
    return {
        "provided_keys": sorted(operator_evidence.keys()),
        "report_catalog_record_archive_artifact_path": (
            operator_evidence.get("report_catalog_record_archive_artifact_path")
            or operator_evidence.get(
                "telegram_dispatch_report_catalog_record_archive_artifact_path"
            )
            or operator_evidence.get("market_intel_report_catalog_record_archive_artifact_path")
            or archive_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 operator_evidence.get(
                "telegram_dispatch_report_catalog_record_closeout_artifact_path"
            )
            or archive_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 archive_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 archive_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 archive_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 archive_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 archive_payload.get("catalog_record_backup_artifact_path")
        ),
        "operator_confirmed_report_catalog_record_archive_gate": bool(
            operator_evidence.get("operator_confirmed_report_catalog_record_archive_gate")
            or operator_evidence.get(
                "operator_confirmed_market_intel_report_catalog_record_archive"
            )
            or archive_payload.get("operator_confirmed_report_catalog_record_archive_gate")
        ),
        "operator_confirmed_report_catalog_record_closeout_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_closeout_reviewed"
            )
            or operator_evidence.get(
                "operator_confirmed_catalog_record_closeout_reviewed"
            )
            or archive_payload.get(
                "operator_confirmed_report_catalog_record_closeout_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_db_commit_observed": bool(
            operator_evidence.get("operator_confirmed_catalog_record_db_commit_observed")
            or operator_evidence.get("operator_confirmed_database_commit_in_run_receipt")
            or archive_payload.get("operator_confirmed_catalog_record_db_commit_observed")
        ),
        "operator_confirmed_catalog_record_no_followup_db_write_required": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_no_followup_db_write_required"
            )
            or operator_evidence.get("operator_confirmed_no_followup_db_write_required")
            or archive_payload.get(
                "operator_confirmed_catalog_record_no_followup_db_write_required"
            )
        ),
        "operator_confirmed_catalog_record_archive_manifest_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_archive_manifest_reviewed"
            )
            or operator_evidence.get("operator_confirmed_archive_manifest_reviewed")
            or archive_payload.get(
                "operator_confirmed_catalog_record_archive_manifest_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_archive_retention_policy_recorded": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_archive_retention_policy_recorded"
            )
            or operator_evidence.get(
                "operator_confirmed_archive_retention_policy_recorded"
            )
            or archive_payload.get(
                "operator_confirmed_catalog_record_archive_retention_policy_recorded"
            )
        ),
        "operator_confirmed_catalog_record_archive_summary_requires_separate_gate": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_archive_summary_requires_separate_gate"
            )
            or operator_evidence.get(
                "operator_confirmed_report_catalog_record_archive_summary_requires_separate_gate"
            )
            or archive_payload.get(
                "operator_confirmed_catalog_record_archive_summary_requires_separate_gate"
            )
        ),
        "operator_confirmed_no_token_in_report_catalog_record_archive": bool(
            operator_evidence.get(
                "operator_confirmed_no_token_in_report_catalog_record_archive"
            )
            or operator_evidence.get("operator_confirmed_no_token_in_artifacts")
            or archive_payload.get(
                "operator_confirmed_no_token_in_report_catalog_record_archive"
            )
        ),
        "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 archive_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 archive_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 archive_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 archive_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 archive_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 archive_payload.get("operator_confirmed_no_api_db_write")
        ),
        "operator_confirmed_no_llm_call": bool(
            operator_evidence.get("operator_confirmed_no_llm_call")
            or archive_payload.get("operator_confirmed_no_llm_call")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            operator_evidence.get("operator_confirmed_no_scheduler_attach")
            or archive_payload.get("operator_confirmed_no_scheduler_attach")
        ),
        "archive_notes_recorded": _has_text(
            operator_evidence.get("report_catalog_record_archive_notes")
            or operator_evidence.get("catalog_record_archive_notes")
            or archive_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(archive_payload),
    }


def _archive_sections(closeout, operator):
    return [
        {
            "key": "catalog_record_archive_identity",
            "title": "Catalog record archive identity",
            "facts": [
                f"family={closeout['writer_record_family'] or 'missing'}",
                f"record_key={closeout['writer_catalog_record_key'] or 'missing'}",
                f"statement_count={closeout['writer_statement_count']}",
            ],
        },
        {
            "key": "catalog_record_archive_closeout",
            "title": "Catalog record closeout review",
            "facts": [
                f"closeout_passed={closeout['closeout_passed']}",
                f"db_commit={closeout['writer_database_commit_executed']}",
                f"smoke_passed={closeout['smoke_postwrite_smoke_passed']}",
            ],
        },
        {
            "key": "catalog_record_archive_operator",
            "title": "Catalog record archive operator confirmations",
            "facts": [
                f"archive_gate={operator['operator_confirmed_report_catalog_record_archive_gate']}",
                f"closeout_reviewed={operator['operator_confirmed_report_catalog_record_closeout_reviewed']}",
                f"archive_summary_gate={operator['operator_confirmed_catalog_record_archive_summary_requires_separate_gate']}",
            ],
        },
        {
            "key": "catalog_record_archive_safety",
            "title": "Catalog record archive safety",
            "facts": [
                "api_cli_executed=false",
                "api_database_write_executed=false",
                "catalog_record_archive_executed=false",
                "scheduler_attached=false",
            ],
        },
    ]


def _archive_gates(closeout, operator, apply_real_write):
    return [
        {
            "key": "report_catalog_record_closeout_passed",
            "label": "上一關 report catalog record closeout gate 已通過",
            "passed": bool(
                closeout["provided"]
                and closeout["mode"]
                == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout_preview"
                and closeout["closeout_passed"]
                and closeout["ready_for_next_manual_phase"]
                and closeout["ready_for_market_intel_report_catalog_record_archive"]
                and not closeout["ready_for_market_intel_report_catalog_record_closeout"]
            ),
        },
        {
            "key": "report_catalog_record_closeout_catalog_record_evidence_retained",
            "label": "closeout payload 保留 catalog record identity、DB commit 與 post-write smoke 證據",
            "passed": bool(
                closeout["writer_catalog_record_key"]
                and closeout["writer_target_table"] == TARGET_TABLE
                and closeout["writer_target_column"] == TARGET_COLUMN
                and closeout["writer_statement_count"] > 0
                and closeout["writer_database_commit_executed"]
                and closeout["writer_hash_match"]
                and closeout["smoke_read_only_query_executed"]
                and closeout["smoke_postwrite_smoke_passed"]
                and closeout["smoke_hash_match"]
            ),
        },
        {
            "key": "report_catalog_record_closeout_operator_evidence_retained",
            "label": "closeout payload 保留操作員 closeout、commit、run receipt、DB commit、備份與 archive separate gate 確認",
            "passed": bool(
                closeout["operator_closeout_gate"]
                and closeout["operator_commit_reviewed"]
                and closeout["operator_db_commit_observed"]
                and closeout["operator_no_followup_db_write_required"]
                and closeout["operator_closeout_summary_ready"]
                and closeout["operator_archive_requires_separate_gate"]
                and closeout["operator_closeout_artifact_path"]
                and closeout["operator_commit_artifact_path_recorded"]
                and closeout["operator_run_receipt_artifact_path_recorded"]
                and closeout["operator_writer_output_artifact_path_recorded"]
                and closeout["operator_postwrite_smoke_artifact_path_recorded"]
                and closeout["operator_backup_artifact_path_recorded"]
            ),
        },
        {
            "key": "report_catalog_record_closeout_promotion_gate",
            "label": "closeout promotion gate 僅放行到 catalog record archive 且要求獨立 gate",
            "passed": bool(
                closeout["promotion_allowed"]
                and closeout["promotion_next_manual_phase"]
                == "market_intel_report_catalog_record_archive"
                and closeout["promotion_archive_separate_gate"]
                and closeout["promotion_api_must_not_generate_report"]
                and closeout["promotion_api_must_not_write_file"]
                and closeout["promotion_api_must_not_execute_cli"]
                and closeout["promotion_api_must_not_write_catalog_record"]
                and closeout["promotion_api_must_not_write_database"]
                and closeout["promotion_api_must_not_commit_catalog_record"]
                and closeout["promotion_api_must_not_dispatch_telegram"]
            ),
        },
        {
            "key": "report_catalog_record_closeout_api_side_effects_blocked",
            "label": "上一關 closeout top-level API 沒有執行 CLI、寫檔、寫 DB、commit、派 Telegram 或掛 scheduler",
            "passed": bool(
                not closeout["api_executes_cli"]
                and not closeout["api_writes_file"]
                and not closeout["api_writes_database"]
                and not closeout["api_dispatches_telegram"]
                and not closeout["database_write_executed"]
                and not closeout["catalog_record_closeout_executed"]
                and not closeout["scheduler_attached"]
                and not closeout["blocked_response_keys_true"]
            ),
        },
        {
            "key": "report_catalog_record_closeout_safe_boundaries_complete",
            "label": "上一關 closeout safe boundaries 完整且未夾帶 token key",
            "passed": bool(
                closeout["safe_boundaries_complete"]
                and not closeout["forbidden_token_key_detected"]
            ),
        },
        {
            "key": "report_catalog_record_archive_operator_confirmed",
            "label": "操作員確認 archive gate、closeout/commit/run receipt/backup artifact 與 archive summary separate gate",
            "passed": bool(
                operator["report_catalog_record_archive_artifact_path"]
                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_archive_gate"]
                and operator[
                    "operator_confirmed_report_catalog_record_closeout_reviewed"
                ]
                and operator["operator_confirmed_catalog_record_db_commit_observed"]
                and operator[
                    "operator_confirmed_catalog_record_no_followup_db_write_required"
                ]
                and operator[
                    "operator_confirmed_catalog_record_archive_manifest_reviewed"
                ]
                and operator[
                    "operator_confirmed_catalog_record_archive_retention_policy_recorded"
                ]
                and operator[
                    "operator_confirmed_catalog_record_archive_summary_requires_separate_gate"
                ]
            ),
        },
        {
            "key": "report_catalog_record_archive_runtime_boundaries_confirmed",
            "label": "操作員確認本 API 不產報表、不寫檔、不執行 CLI、不寫 DB、不派 Telegram、不呼叫 LLM、不掛 scheduler",
            "passed": bool(
                operator[
                    "operator_confirmed_no_token_in_report_catalog_record_archive"
                ]
                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_archive_no_token_submitted_to_api",
            "label": "archive gate payload 不得包含 approval 或 Telegram token key",
            "passed": not operator["forbidden_token_submitted_to_api"],
        },
        {
            "key": "report_catalog_record_archive_apply_real_write_not_requested_from_api",
            "label": "API/UI report catalog record archive gate 不接受 apply_real_write",
            "passed": not apply_real_write,
        },
    ]


def build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(
    *,
    telegram_dispatch_report_catalog_record_closeout,
    report_catalog_record_archive=None,
    operator_evidence=None,
    execute_requested=False,
    apply_real_write=False,
):
    """建立 Telegram dispatch report catalog record archive gate；不執行副作用。"""
    closeout = _closeout_summary(telegram_dispatch_report_catalog_record_closeout)
    operator = _operator_summary(operator_evidence, report_catalog_record_archive)
    sections = _archive_sections(closeout, operator)
    gates = _archive_gates(closeout, operator, bool(apply_real_write))
    blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
    archive_passed = bool(not blocked_reasons)

    return {
        "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_preview",
        "target_table": TARGET_TABLE,
        "target_column": TARGET_COLUMN,
        "target_json_path": [
            "market_intel_ai_summary",
            "telegram_dispatch_report_catalog_record_archive",
        ],
        "target_operation": "review_manual_market_intel_report_catalog_record_archive",
        "execute_requested": bool(execute_requested),
        "apply_real_write_requested": bool(apply_real_write),
        "report_catalog_record_archive_reviewed": True,
        "telegram_dispatch_report_catalog_record_archive_passed": archive_passed,
        "summary_persistence_telegram_dispatch_report_catalog_record_archive_passed": archive_passed,
        "report_catalog_record_archive_passed": archive_passed,
        "ready_for_next_manual_phase": archive_passed,
        "ready_for_market_intel_report_catalog_record_archive_summary": archive_passed,
        "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": closeout["writer_statement_count"]
        or closeout["statement_count"],
        "expected_summary_payload_hash": closeout["expected_summary_payload_hash"],
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "telegram_dispatch_report_catalog_record_closeout": closeout,
        "telegram_dispatch_report_catalog_record_archive_sections": sections,
        "operator_telegram_dispatch_report_catalog_record_archive": operator,
        "promotion_gate": {
            "allowed": archive_passed,
            "next_manual_phase": "market_intel_report_catalog_record_archive_summary",
            "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_archive_summary_requires_separate_gate": True,
        },
        "next_operator_steps": [
            "保存 report catalog record archive gate artifact path",
            "確認 closeout、commit、run receipt、writer output、post-write smoke 與備份都已封存",
            "只在 archive gate 通過後進入 report catalog record archive summary gate",
            "不得從 API/UI 產報表、寫檔、補寫 catalog record、commit、派送 Telegram 或掛 scheduler",
        ],
        "safe_boundaries": [
            "do_not_read_approval_token_from_report_catalog_record_archive_api",
            "do_not_read_telegram_token_from_report_catalog_record_archive_api",
            "do_not_call_llm_from_report_catalog_record_archive",
            "do_not_generate_report_from_report_catalog_record_archive_api",
            "do_not_write_report_catalog_record_archive_artifact_from_api",
            "do_not_execute_catalog_record_cli_from_report_catalog_record_archive_api",
            "do_not_write_report_catalog_record_from_report_catalog_record_archive_api",
            "do_not_commit_catalog_record_from_report_catalog_record_archive_api",
            "do_not_dispatch_telegram_from_report_catalog_record_archive_api",
            "do_not_open_database_connection_from_report_catalog_record_archive",
            "do_not_update_review_state_from_report_catalog_record_archive",
            "do_not_attach_scheduler_from_report_catalog_record_archive",
            "future_market_intel_report_catalog_record_archive_summary_must_use_separate_gate",
            "report_catalog_record_archive_preview_only",
            "no_remove_orphans",
            "no_momo_db_lifecycle_change",
        ],
    }
