"""候選審核 queue AI summary Telegram dispatch report run readiness preview。

本模組只檢查 report run package 後的報表執行準備度；不讀 approval 或
Telegram token、不呼叫 LLM、不派送 Telegram、不開 DB、不寫檔、不產報表、
不更新 review_state、不掛 scheduler。
"""


FORBIDDEN_TOKEN_KEYWORDS = (
    "approval_token",
    "approval-token",
    "market_intel_queue_write_approval",
    "telegram_bot_token",
    "telegram-token",
    "telegram_token",
    "bot_token",
)
SAFE_TOKEN_METADATA_KEYS = {
    "approval_token_present",
    "approval_token_valid",
    "approval_token_secret_configured",
    "telegram_token_present",
    "telegram_token_configured",
}
SAFE_APPROVAL_ENV_VAR = "MARKET_INTEL_QUEUE_WRITE_APPROVAL"
TARGET_TABLE = "market_alert_review_queue"
TARGET_COLUMN = "metadata_json"
SUMMARY_METADATA_KEY = "ai_summary_review"


def _as_dict(value):
    return value if isinstance(value, dict) else {}


def _as_list(value):
    if value is None:
        return []
    if isinstance(value, (list, tuple, set)):
        return list(value)
    return [value]


def _safe_int(value):
    try:
        return int(value or 0)
    except (TypeError, ValueError):
        return 0


def _safe_text(value, limit=300):
    if value is None:
        return None
    text = str(value).strip()
    return text[:limit] if text else None


def _has_text(value):
    return bool(isinstance(value, str) and value.strip())


def _contains_forbidden_token_key(value):
    if isinstance(value, dict):
        for key, nested in value.items():
            normalized_key = str(key).lower()
            if normalized_key in SAFE_TOKEN_METADATA_KEYS and isinstance(nested, bool):
                continue
            if normalized_key == "approval_env_var" and nested == SAFE_APPROVAL_ENV_VAR:
                continue
            if any(token_key in normalized_key for token_key in FORBIDDEN_TOKEN_KEYWORDS):
                return True
            if _contains_forbidden_token_key(nested):
                return True
    elif isinstance(value, list):
        return any(_contains_forbidden_token_key(item) for item in value)
    return False


def _side_effects_clear(*payloads):
    blocked_keys = (
        "ready_for_report_generation",
        "ready_for_telegram_dispatch",
        "ready_for_manual_telegram_dispatch",
        "ready_for_api_database_write",
        "ready_for_scheduler_attach",
        "ready_for_llm_call",
        "api_executes_cli",
        "api_executes_llm",
        "api_dispatches_telegram",
        "api_reads_approval_token",
        "api_writes_file",
        "api_writes_database",
        "api_updates_review_state",
        "telegram_dispatch_report_run_readiness_file_written",
        "report_run_readiness_file_written",
        "telegram_dispatch_report_run_package_file_written",
        "report_run_package_file_written",
        "telegram_dispatch_report_input_file_written",
        "report_input_file_written",
        "report_file_written",
        "report_record_written",
        "report_manifest_written",
        "summary_file_written",
        "summary_record_written",
        "summary_manifest_written",
        "ai_summary_generated",
        "llm_call_executed",
        "ollama_call_executed",
        "gemini_call_executed",
        "telegram_dispatched",
        "telegram_dispatch_attempted",
        "external_network_executed",
        "database_connection_opened",
        "database_write_executed",
        "database_commit_executed",
        "review_state_update_executed",
        "scheduler_attached",
        "writes_executed",
        "would_write_database",
    )
    return all(
        not _as_dict(payload).get(key)
        for payload in payloads
        for key in blocked_keys
    )


def _package_summary(telegram_dispatch_report_run_package):
    package = _as_dict(telegram_dispatch_report_run_package)
    contract = _as_dict(package.get("telegram_dispatch_report_run_package"))
    evidence_refs = _as_dict(contract.get("evidence_refs"))
    artifact_paths = _as_dict(evidence_refs.get("artifact_paths"))
    sections = [
        _as_dict(section)
        for section in _as_list(
            package.get("telegram_dispatch_report_run_package_sections")
        )
    ]
    section_keys = [section.get("key") for section in sections if section.get("key")]
    execution_boundaries = _as_dict(contract.get("execution_boundaries"))
    return {
        "provided": bool(package),
        "mode": package.get("mode"),
        "target_operation": package.get("target_operation"),
        "telegram_dispatch_report_run_package_ready": bool(
            package.get("telegram_dispatch_report_run_package_ready")
        ),
        "summary_persistence_telegram_dispatch_report_run_package_ready": bool(
            package.get(
                "summary_persistence_telegram_dispatch_report_run_package_ready"
            )
        ),
        "report_run_package_ready": bool(package.get("report_run_package_ready")),
        "ready_for_market_intel_report_run_readiness": bool(
            package.get("ready_for_market_intel_report_run_readiness")
        ),
        "ready_for_next_manual_phase": bool(
            package.get("ready_for_next_manual_phase")
        ),
        "ready_for_report_generation": bool(package.get("ready_for_report_generation")),
        "statement_count": _safe_int(package.get("statement_count")),
        "expected_summary_payload_hash": package.get(
            "expected_summary_payload_hash"
        ),
        "package_contract": contract,
        "package_contract_version": contract.get("contract_version"),
        "source_contract_version": contract.get("source_contract_version"),
        "target_report_family": contract.get("target_report_family"),
        "language": contract.get("language"),
        "required_package_sections": _as_list(
            contract.get("required_package_sections")
        ),
        "source_required_sections": _as_list(contract.get("source_required_sections")),
        "intended_report_artifacts": _as_list(
            contract.get("intended_report_artifacts")
        ),
        "package_sections": sections,
        "package_section_keys": section_keys,
        "evidence_refs": {
            "message_id": _safe_text(evidence_refs.get("message_id"), 120),
            "chat_id": _safe_text(evidence_refs.get("chat_id"), 120),
            "telegram_channel_label": _safe_text(
                evidence_refs.get("telegram_channel_label"),
                180,
            ),
            "summary_payload_hash": _safe_text(
                evidence_refs.get("summary_payload_hash"),
                80,
            ),
            "artifact_paths": artifact_paths,
        },
        "execution_boundaries": execution_boundaries,
        "blocked_reasons": _as_list(package.get("blocked_reasons")),
        "safe_boundaries": _as_list(package.get("safe_boundaries")),
        "side_effects_clear": _side_effects_clear(package),
    }


def _operator_summary(operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    artifact_paths = {
        "telegram_dispatch_report_run_package_artifact_path": _safe_text(
            operator_evidence.get("telegram_dispatch_report_run_package_artifact_path")
            or operator_evidence.get("report_run_package_artifact_path")
        ),
        "telegram_dispatch_report_run_readiness_artifact_path": _safe_text(
            operator_evidence.get(
                "telegram_dispatch_report_run_readiness_artifact_path"
            )
            or operator_evidence.get("report_run_readiness_artifact_path")
        ),
        "report_output_artifact_path": _safe_text(
            operator_evidence.get("report_output_artifact_path")
            or operator_evidence.get("telegram_dispatch_report_output_artifact_path")
        ),
    }
    return {
        "provided_keys": sorted(operator_evidence.keys()),
        **artifact_paths,
        "artifact_path_count": sum(
            1 for value in artifact_paths.values() if _has_text(value)
        ),
        "operator_confirmed_telegram_dispatch_report_run_readiness": bool(
            operator_evidence.get(
                "operator_confirmed_telegram_dispatch_report_run_readiness"
            )
            or operator_evidence.get(
                "operator_confirmed_market_intel_report_run_readiness"
            )
        ),
        "operator_confirmed_report_run_package_reviewed": bool(
            operator_evidence.get("operator_confirmed_report_run_package_reviewed")
            or operator_evidence.get("operator_confirmed_report_package_reviewed")
        ),
        "operator_confirmed_report_contract_reviewed": bool(
            operator_evidence.get("operator_confirmed_report_contract_reviewed")
        ),
        "operator_confirmed_report_generation_is_manual_only": bool(
            operator_evidence.get("operator_confirmed_report_generation_is_manual_only")
            or operator_evidence.get("operator_confirmed_manual_report_generation_only")
        ),
        "operator_confirmed_report_output_requires_receipt": bool(
            operator_evidence.get("operator_confirmed_report_output_requires_receipt")
        ),
        "operator_confirmed_no_token_in_report_readiness": bool(
            operator_evidence.get("operator_confirmed_no_token_in_report_readiness")
            or operator_evidence.get("operator_confirmed_no_token_in_artifacts")
        ),
        "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")
        ),
        "operator_confirmed_no_api_telegram_dispatch": bool(
            operator_evidence.get("operator_confirmed_no_api_telegram_dispatch")
        ),
        "operator_confirmed_no_api_db_write": bool(
            operator_evidence.get("operator_confirmed_no_api_db_write")
        ),
        "operator_confirmed_no_llm_call": bool(
            operator_evidence.get("operator_confirmed_no_llm_call")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            operator_evidence.get("operator_confirmed_no_scheduler_attach")
        ),
        "report_run_readiness_notes_recorded": bool(
            _safe_text(operator_evidence.get("report_run_readiness_notes"))
            or _safe_text(operator_evidence.get("report_run_package_notes"))
        ),
        "forbidden_token_submitted_to_api": _contains_forbidden_token_key(
            operator_evidence
        ),
    }


def _report_generation_manifest(package):
    evidence_refs = package["evidence_refs"]
    return {
        "manifest_version": "market_intel_report_run_readiness_v1",
        "source_mode": package["mode"],
        "source_contract_version": package["package_contract_version"],
        "target_report_family": package["target_report_family"],
        "language": package["language"],
        "statement_count": package["statement_count"],
        "expected_summary_payload_hash": package["expected_summary_payload_hash"],
        "required_package_sections": package["required_package_sections"],
        "source_required_sections": package["source_required_sections"],
        "intended_report_artifacts": package["intended_report_artifacts"],
        "evidence_refs": evidence_refs,
        "manual_generation_command": {
            "key": "manual_market_intel_report_generation",
            "command_shape": (
                "python3 scripts/market_intel_report_generator.py "
                "--input artifacts/market_intel/telegram-dispatch-report-run-package.json "
                "--output artifacts/market_intel/telegram-dispatch-audit-report.json "
                "--manual-operator-approved"
            ),
            "executes_report_generation": True,
            "executes_database": False,
            "executes_telegram": False,
            "executed_by_api": False,
        },
        "execution_boundaries": {
            "api_generates_report": False,
            "api_writes_report_file": False,
            "api_calls_llm": False,
            "api_dispatches_telegram": False,
            "api_writes_database": False,
            "api_attaches_scheduler": False,
        },
    }


def _readiness_gates(package, operator, manifest, apply_real_write):
    required_package_sections = {
        "source_report_input",
        "report_contract",
        "evidence_manifest",
        "execution_boundaries",
        "future_readiness_gate",
    }
    package_section_keys = set(package["package_section_keys"])
    evidence = package["evidence_refs"]
    boundaries = package["execution_boundaries"]
    return [
        {
            "key": "telegram_dispatch_report_run_package_ready",
            "label": "Telegram dispatch report run package 必須已通過並放行到 report run readiness",
            "passed": bool(
                package["mode"]
                == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package_preview"
                and package["telegram_dispatch_report_run_package_ready"]
                and package[
                    "summary_persistence_telegram_dispatch_report_run_package_ready"
                ]
                and package["report_run_package_ready"]
                and package["ready_for_market_intel_report_run_readiness"]
                and package["ready_for_next_manual_phase"]
                and not package["ready_for_report_generation"]
            ),
        },
        {
            "key": "telegram_dispatch_report_run_package_contract_complete",
            "label": "run package contract 必須完整保留 report family、來源 contract 與 evidence refs",
            "passed": bool(
                package["package_contract_version"]
                == "market_intel_telegram_dispatch_report_run_package_v1"
                and package["source_contract_version"]
                == "market_intel_telegram_dispatch_report_input_v1"
                and package["target_report_family"]
                and package["language"] == "zh-TW"
                and evidence["message_id"]
                and evidence["chat_id"]
                and evidence["telegram_channel_label"]
                and evidence["summary_payload_hash"]
            ),
        },
        {
            "key": "telegram_dispatch_report_run_package_sections_complete",
            "label": "run package sections 與 report generation manifest 必須完整",
            "passed": bool(
                required_package_sections.issubset(package_section_keys)
                and manifest["manifest_version"]
                == "market_intel_report_run_readiness_v1"
                and manifest["manual_generation_command"]["executed_by_api"] is False
            ),
        },
        {
            "key": "telegram_dispatch_report_generation_boundaries_clear",
            "label": "run package 與 manifest 必須禁止 API 產報表、寫檔、LLM、Telegram、DB 與 scheduler",
            "passed": bool(
                not boundaries.get("api_generates_report")
                and not boundaries.get("api_writes_report_file")
                and not boundaries.get("api_calls_llm")
                and not boundaries.get("api_dispatches_telegram")
                and not boundaries.get("api_writes_database")
                and not boundaries.get("api_attaches_scheduler")
                and not manifest["execution_boundaries"]["api_generates_report"]
                and not manifest["execution_boundaries"]["api_writes_report_file"]
                and not manifest["execution_boundaries"]["api_calls_llm"]
                and not manifest["execution_boundaries"]["api_dispatches_telegram"]
                and not manifest["execution_boundaries"]["api_writes_database"]
                and not manifest["execution_boundaries"]["api_attaches_scheduler"]
            ),
        },
        {
            "key": "report_run_readiness_artifacts_recorded",
            "label": "run package、readiness 與 report output artifact path 必須齊備",
            "passed": bool(
                operator["telegram_dispatch_report_run_package_artifact_path"]
                and operator["telegram_dispatch_report_run_readiness_artifact_path"]
                and operator["report_output_artifact_path"]
            ),
        },
        {
            "key": "operator_confirmed_telegram_dispatch_report_run_readiness",
            "label": "操作員確認 report readiness、package、contract、人工產報表與 receipt 邊界",
            "passed": bool(
                operator[
                    "operator_confirmed_telegram_dispatch_report_run_readiness"
                ]
                and operator["operator_confirmed_report_run_package_reviewed"]
                and operator["operator_confirmed_report_contract_reviewed"]
                and operator["operator_confirmed_report_generation_is_manual_only"]
                and operator["operator_confirmed_report_output_requires_receipt"]
            ),
        },
        {
            "key": "operator_confirmed_report_run_readiness_runtime_boundaries",
            "label": "操作員確認本階段不產報表、不派送 Telegram、不寫 DB、不呼叫 LLM、不掛 scheduler",
            "passed": bool(
                operator["operator_confirmed_no_token_in_report_readiness"]
                and operator["operator_confirmed_no_api_report_generation"]
                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": "telegram_dispatch_report_run_readiness_no_token_submitted_to_api",
            "label": "payload 不得包含 approval 或 Telegram token key",
            "passed": not operator["forbidden_token_submitted_to_api"],
        },
        {
            "key": "telegram_dispatch_report_run_readiness_apply_real_write_not_requested_from_api",
            "label": "API/UI report run readiness 不接受 apply_real_write",
            "passed": not apply_real_write,
        },
        {
            "key": "telegram_dispatch_report_run_package_has_no_side_effects",
            "label": "上游 run package 不得已有報表、DB、檔案、LLM、Telegram 或 scheduler 副作用",
            "passed": package["side_effects_clear"],
        },
    ]


def build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness(
    *,
    telegram_dispatch_report_run_package,
    operator_evidence=None,
    execute_requested=False,
    apply_real_write=False,
):
    """建立 Telegram dispatch report run readiness gate；不執行任何副作用。"""
    package = _package_summary(telegram_dispatch_report_run_package)
    operator = _operator_summary(operator_evidence)
    manifest = _report_generation_manifest(package)
    gates = _readiness_gates(
        package,
        operator,
        manifest,
        bool(apply_real_write),
    )
    blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
    readiness_ready = bool(not blocked_reasons)

    return {
        "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness_preview",
        "target_table": TARGET_TABLE,
        "target_column": TARGET_COLUMN,
        "target_json_path": [
            SUMMARY_METADATA_KEY,
            "telegram_dispatch_report_run_readiness",
        ],
        "target_operation": "manual_market_intel_report_run_readiness",
        "execute_requested": bool(execute_requested),
        "apply_real_write_requested": bool(apply_real_write),
        "report_run_readiness_reviewed": True,
        "telegram_dispatch_report_run_readiness_ready": readiness_ready,
        "summary_persistence_telegram_dispatch_report_run_readiness_ready": readiness_ready,
        "report_run_readiness_ready": readiness_ready,
        "ready_for_next_manual_phase": readiness_ready,
        "ready_for_market_intel_report_run_receipt": readiness_ready,
        "ready_for_manual_report_generation": readiness_ready,
        "ready_for_report_generation": False,
        "ready_for_ai_summary_generation": False,
        "ready_for_llm_call": False,
        "ready_for_telegram_dispatch": False,
        "ready_for_manual_telegram_dispatch": False,
        "ready_for_api_database_write": False,
        "ready_for_scheduler_attach": False,
        "ready_for_real_write": 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,
        "telegram_dispatch_report_run_readiness_file_written": False,
        "report_run_readiness_file_written": False,
        "telegram_dispatch_report_run_package_file_written": False,
        "report_run_package_file_written": False,
        "telegram_dispatch_report_input_file_written": False,
        "report_input_file_written": False,
        "report_file_written": False,
        "report_record_written": False,
        "report_manifest_written": False,
        "summary_file_written": False,
        "summary_record_written": False,
        "summary_manifest_written": False,
        "summary_persistence_record_written": False,
        "metadata_patch_written": False,
        "ai_summary_generated": False,
        "llm_call_executed": False,
        "ollama_call_executed": False,
        "gemini_call_executed": False,
        "telegram_dispatched": False,
        "telegram_dispatch_attempted": False,
        "review_state_update_executed": False,
        "read_only_query_executed": False,
        "database_connection_opened": False,
        "database_session_created": False,
        "explicit_transaction_opened": False,
        "transaction_opened": False,
        "transaction_committed": False,
        "database_write_executed": False,
        "database_commit_executed": False,
        "database_rollback_executed": False,
        "external_network_executed": False,
        "scheduler_attached": False,
        "writes_executed": False,
        "would_write_database": False,
        "statement_count": package["statement_count"],
        "expected_summary_payload_hash": package["expected_summary_payload_hash"],
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "telegram_dispatch_report_run_package_summary": package,
        "report_generation_readiness_manifest": manifest,
        "operator_telegram_dispatch_report_run_readiness": operator,
        "promotion_gate": {
            "allowed": readiness_ready,
            "next_manual_phase": "market_intel_report_run_receipt",
            "requires_real_db_write": False,
            "requires_scheduler_attach": False,
            "requires_operator_approval": True,
            "future_report_generation_result_requires_receipt": True,
            "api_must_not_generate_report": True,
            "api_must_not_dispatch_telegram": True,
        },
        "next_operator_steps": [
            "保存 Telegram dispatch report run readiness artifact path",
            "人工或後續獨立 job 依 run package 產生 report output，不透過本 API 寫檔",
            "保存 report output artifact path 與 generation receipt",
            "下一階段 report run receipt 才審核產出結果；API 不補產、不重跑、不掛 scheduler",
        ],
        "safe_boundaries": [
            "do_not_read_approval_token_from_telegram_dispatch_report_run_readiness_api",
            "do_not_read_telegram_token_from_telegram_dispatch_report_run_readiness_api",
            "do_not_call_llm_from_telegram_dispatch_report_run_readiness",
            "do_not_generate_report_from_telegram_dispatch_report_run_readiness_api",
            "do_not_dispatch_telegram_from_telegram_dispatch_report_run_readiness_api",
            "do_not_open_database_connection_from_telegram_dispatch_report_run_readiness",
            "do_not_write_report_artifact_from_telegram_dispatch_report_run_readiness_api",
            "do_not_update_review_state_from_telegram_dispatch_report_run_readiness",
            "do_not_attach_scheduler_from_telegram_dispatch_report_run_readiness",
            "future_report_generation_result_must_use_report_run_receipt",
            "telegram_dispatch_report_run_readiness_preview_only",
            "no_remove_orphans",
            "no_momo_db_lifecycle_change",
        ],
    }
