"""市場情報 MCP fetch candidate handoff review preview。

本模組只審核 parser review 後的候選交接包；不建立 queue、不寫 DB、
不讀 artifact、不發 HTTP request、不掛 scheduler。
"""

from services.market_intel.mcp_fetch_run_package import RUN_ARTIFACT_DIR_PREFIX
from services.market_intel.mcp_fetch_result_parser_review import (
    build_mcp_fetch_result_parser_review_preview,
)


MAX_HANDOFF_CANDIDATES = 80
SAFE_HANDOFF_STATUSES = {"queued_for_operator_review"}
SAFE_TARGET_QUEUES = {"manual_candidate_review"}
SAFE_DEDUPE_STRATEGIES = {"platform_source_candidate_key"}

FORBIDDEN_SECRET_KEYS = (
    "approval_token",
    "approval-token",
    "api_key",
    "authorization",
    "bearer",
    "client_secret",
    "cookie",
    "password",
    "secret",
    "session_cookie",
    "token",
)

SAFE_SECRET_METADATA_KEYS = {
    "operator_confirmed_no_secret_payload",
}

FORBIDDEN_RAW_PAYLOAD_KEYS = (
    "body_html",
    "document_html",
    "html",
    "page_html",
    "raw_body",
    "raw_html",
    "raw_snapshot",
    "response_body",
)

_BLOCKED_SIDE_EFFECT_KEYS = (
    "allow_api_execution",
    "allow_database_write",
    "allow_external_network_in_api",
    "allow_scheduler_attach",
    "api_executed_cli",
    "api_executes_cli",
    "api_executes_docker",
    "api_executes_health_check",
    "api_executes_ssh",
    "api_opens_database_connection",
    "api_uses_external_network",
    "api_writes_database",
    "attach_scheduler",
    "candidate_handoff_persisted",
    "candidate_queue_created",
    "candidate_queue_persisted",
    "cli_executed",
    "command_executed",
    "database_commit_executed",
    "database_session_created",
    "database_write_executed",
    "external_network_executed",
    "fetch_executed",
    "file_written",
    "manual_fetch_gate_opened_by_api",
    "network_request_allowed",
    "payload_persisted",
    "scheduler_attached",
    "write_database",
    "writes_executed",
    "would_write_database",
)


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 _has_text(value):
    return bool(isinstance(value, str) and value.strip())


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


def _safe_artifact_path(value, *, require_json=False):
    if not isinstance(value, str):
        return False
    normalized = value.strip().replace("\\", "/")
    if not normalized or normalized.startswith("/"):
        return False
    parts = [part for part in normalized.split("/") if part]
    if any(part in (".", "..") for part in parts):
        return False
    if not normalized.startswith(RUN_ARTIFACT_DIR_PREFIX):
        return False
    return not require_json or normalized.endswith(".json")


def _contains_forbidden_key(value, forbidden_keys, *, safe_keys=None):
    safe_keys = safe_keys or set()
    if isinstance(value, dict):
        for key, nested in value.items():
            normalized_key = str(key).lower()
            if normalized_key in safe_keys and isinstance(nested, bool):
                continue
            if any(forbidden_key in normalized_key for forbidden_key in forbidden_keys):
                return True
            if _contains_forbidden_key(nested, forbidden_keys, safe_keys=safe_keys):
                return True
    elif isinstance(value, list):
        return any(
            _contains_forbidden_key(item, forbidden_keys, safe_keys=safe_keys)
            for item in value
        )
    return False


def _blocked_side_effects(payload):
    found = []

    def visit(value, path):
        if isinstance(value, dict):
            for key, item in value.items():
                normalized_key = str(key).lower()
                key_path = f"{path}.{key}" if path else key
                if normalized_key in _BLOCKED_SIDE_EFFECT_KEYS and bool(item):
                    found.append(key_path)
                visit(item, key_path)
        elif isinstance(value, list):
            for index, item in enumerate(value):
                visit(item, f"{path}[{index}]")

    visit(payload, "")
    return found


def _parser_review_from_inputs(parser_review_package, parser_review_result, phase):
    if isinstance(parser_review_result, dict) and parser_review_result:
        return parser_review_result

    parser_review_package = _as_dict(parser_review_package)
    return build_mcp_fetch_result_parser_review_preview(
        run_receipt_package=(
            parser_review_package.get("run_receipt_package")
            or parser_review_package.get("run_receipt")
            or parser_review_package.get("receipt_review")
        ),
        run_receipt_result=(
            parser_review_package.get("run_receipt_result")
            or parser_review_package.get("mcp_fetch_run_receipt")
        ),
        parser_result=(
            parser_review_package.get("parser_result")
            or parser_review_package.get("fetch_result_parser")
            or parser_review_package.get("parsed_result")
        ),
        phase=phase,
    )


def _candidate_identity(candidate, candidate_type):
    candidate = _as_dict(candidate)
    if candidate_type == "campaign":
        return candidate.get("campaign_key")
    return candidate.get("platform_product_id")


def _sample_candidate_handoff_package():
    parser_preview = build_mcp_fetch_result_parser_review_preview()
    parser_review_package = parser_preview["sample_parser_result_package"]
    parser_review_result = build_mcp_fetch_result_parser_review_preview(
        run_receipt_package=parser_review_package["run_receipt_package"],
        run_receipt_result=parser_review_package["run_receipt_result"],
        parser_result=parser_review_package["parser_result"],
    )
    parser_summary = parser_review_result["parser_result_summary"]
    source_groups = []
    for source in parser_summary.get("source_summaries", []):
        platform_code = source.get("platform_code")
        source_key = source.get("source_key")
        campaigns = [
            item for item in parser_summary.get("campaign_candidates", [])
            if item.get("platform_code") == platform_code
            and item.get("source_key") == source_key
        ]
        products = [
            item for item in parser_summary.get("product_candidates", [])
            if item.get("platform_code") == platform_code
            and item.get("source_key") == source_key
        ]
        source_groups.append(
            {
                "platform_code": platform_code,
                "source_key": source_key,
                "receipt_path": source.get("receipt_path"),
                "parser_artifact_path": source.get("parser_artifact_path"),
                "handoff_status": "queued_for_operator_review",
                "candidate_review_scope": "campaign_and_product_identity_preview",
                "campaign_candidate_count": len(campaigns),
                "product_candidate_count": len(products),
                "campaign_candidate_keys": [
                    item.get("campaign_key") for item in campaigns
                ],
                "product_candidate_keys": [
                    item.get("platform_product_id") for item in products
                ],
                "campaign_candidates": campaigns,
                "product_candidates": products,
            }
        )

    return {
        "parser_review_package": parser_review_package,
        "parser_review_result": parser_review_result,
        "candidate_handoff": {
            "handoff_id": "market-intel-candidate-handoff-sample",
            "handoff_mode": "operator_review_preview",
            "target_queue": "manual_candidate_review",
            "queue_policy": {
                "target_queue": "manual_candidate_review",
                "dedupe_strategy": "platform_source_candidate_key",
                "max_candidates_per_handoff": MAX_HANDOFF_CANDIDATES,
                "write_mode": "preview_only",
            },
            "operator_confirmed_no_database_write": True,
            "operator_confirmed_no_external_network": True,
            "operator_confirmed_no_scheduler_attach": True,
            "operator_confirmed_no_persistence": True,
            "operator_confirmed_manual_review_required": True,
            "operator_confirmed_no_secret_payload": True,
            "summary": {
                "source_count": len(source_groups),
                "campaign_candidate_count": parser_summary.get(
                    "campaign_candidate_count",
                    0,
                ),
                "product_candidate_count": parser_summary.get(
                    "product_candidate_count",
                    0,
                ),
                "candidate_count": parser_summary.get("candidate_count", 0),
            },
            "candidate_groups": source_groups,
        },
    }


def _parser_summary(parser_review):
    parser_review = _as_dict(parser_review)
    side_effects_clear = bool(
        not parser_review.get("manual_fetch_gate_opened_by_api")
        and not parser_review.get("network_request_allowed")
        and not parser_review.get("parser_executed_by_api")
        and not parser_review.get("api_executes_cli")
        and not parser_review.get("api_opens_database_connection")
        and not parser_review.get("api_writes_database")
        and not parser_review.get("api_uses_external_network")
        and not parser_review.get("database_write_executed")
        and not parser_review.get("fetch_executed")
        and not parser_review.get("cli_executed")
        and not parser_review.get("file_written")
        and not parser_review.get("scheduler_attached")
    )
    parser_result_summary = _as_dict(parser_review.get("parser_result_summary"))
    return {
        "mode": parser_review.get("mode"),
        "accepted": bool(
            parser_review.get("mcp_fetch_result_parser_review_accepted")
        ),
        "ready_for_manual_fetch_candidate_handoff_review": bool(
            parser_review.get("ready_for_manual_fetch_candidate_handoff_review")
        ),
        "source_count": _safe_int(parser_review.get("parser_source_count")),
        "campaign_candidate_count": _safe_int(
            parser_review.get("campaign_candidate_count")
        ),
        "product_candidate_count": _safe_int(
            parser_review.get("product_candidate_count")
        ),
        "candidate_count": _safe_int(parser_review.get("candidate_count")),
        "source_keys": sorted(
            tuple(item)
            for item in _as_list(parser_result_summary.get("source_keys"))
            if isinstance(item, (list, tuple)) and len(item) == 2
        ),
        "campaign_candidate_keys": sorted(
            item.get("campaign_key")
            for item in _as_list(parser_result_summary.get("campaign_candidates"))
            if item.get("campaign_key")
        ),
        "product_candidate_keys": sorted(
            item.get("platform_product_id")
            for item in _as_list(parser_result_summary.get("product_candidates"))
            if item.get("platform_product_id")
        ),
        "all_candidate_urls_public": bool(
            parser_result_summary.get("all_candidate_urls_public")
        ),
        "all_candidate_required_fields_present": bool(
            parser_result_summary.get("all_candidate_required_fields_present")
        ),
        "side_effects_clear": side_effects_clear,
        "blocked_reasons": parser_review.get("blocked_reasons", []),
    }


def _handoff_summary(candidate_handoff):
    candidate_handoff = _as_dict(candidate_handoff)
    groups = [_as_dict(item) for item in _as_list(
        candidate_handoff.get("candidate_groups")
    )]
    queue_policy = _as_dict(candidate_handoff.get("queue_policy"))
    campaign_candidates = []
    product_candidates = []
    group_summaries = []
    for group in groups:
        campaigns = [_as_dict(item) for item in _as_list(
            group.get("campaign_candidates")
        )]
        products = [_as_dict(item) for item in _as_list(
            group.get("product_candidates")
        )]
        campaign_candidates.extend(campaigns)
        product_candidates.extend(products)
        group_summaries.append(
            {
                "platform_code": _safe_text(group.get("platform_code"), 80),
                "source_key": _safe_text(group.get("source_key"), 160),
                "receipt_path": _safe_text(group.get("receipt_path"), 500),
                "receipt_path_safe": _safe_artifact_path(
                    group.get("receipt_path"),
                    require_json=True,
                ),
                "parser_artifact_path": _safe_text(
                    group.get("parser_artifact_path"),
                    500,
                ),
                "parser_artifact_path_safe": _safe_artifact_path(
                    group.get("parser_artifact_path"),
                    require_json=True,
                ),
                "handoff_status": _safe_text(group.get("handoff_status"), 120),
                "candidate_review_scope": _safe_text(
                    group.get("candidate_review_scope"),
                    160,
                ),
                "campaign_candidate_count": _safe_int(
                    group.get("campaign_candidate_count")
                ),
                "product_candidate_count": _safe_int(
                    group.get("product_candidate_count")
                ),
                "observed_campaign_candidate_count": len(campaigns),
                "observed_product_candidate_count": len(products),
                "campaign_candidate_keys": sorted(
                    key for key in _as_list(group.get("campaign_candidate_keys")) if key
                ),
                "observed_campaign_candidate_keys": sorted(
                    _candidate_identity(item, "campaign")
                    for item in campaigns
                    if _candidate_identity(item, "campaign")
                ),
                "product_candidate_keys": sorted(
                    key for key in _as_list(group.get("product_candidate_keys")) if key
                ),
                "observed_product_candidate_keys": sorted(
                    _candidate_identity(item, "product")
                    for item in products
                    if _candidate_identity(item, "product")
                ),
            }
        )

    summary = _as_dict(candidate_handoff.get("summary"))
    campaign_count = len(campaign_candidates)
    product_count = len(product_candidates)
    return {
        "provided_keys": sorted(candidate_handoff.keys()),
        "handoff_id": _safe_text(candidate_handoff.get("handoff_id"), 160),
        "handoff_mode": _safe_text(candidate_handoff.get("handoff_mode"), 120),
        "target_queue": _safe_text(candidate_handoff.get("target_queue"), 160),
        "queue_policy": {
            "target_queue": _safe_text(queue_policy.get("target_queue"), 160),
            "dedupe_strategy": _safe_text(queue_policy.get("dedupe_strategy"), 160),
            "max_candidates_per_handoff": _safe_int(
                queue_policy.get("max_candidates_per_handoff")
            ),
            "write_mode": _safe_text(queue_policy.get("write_mode"), 80),
        },
        "operator_confirmed_no_database_write": bool(
            candidate_handoff.get("operator_confirmed_no_database_write")
        ),
        "operator_confirmed_no_external_network": bool(
            candidate_handoff.get("operator_confirmed_no_external_network")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            candidate_handoff.get("operator_confirmed_no_scheduler_attach")
        ),
        "operator_confirmed_no_persistence": bool(
            candidate_handoff.get("operator_confirmed_no_persistence")
        ),
        "operator_confirmed_manual_review_required": bool(
            candidate_handoff.get("operator_confirmed_manual_review_required")
        ),
        "operator_confirmed_no_secret_payload": bool(
            candidate_handoff.get("operator_confirmed_no_secret_payload")
        ),
        "source_count": len(group_summaries),
        "summary_source_count": _safe_int(summary.get("source_count")),
        "campaign_candidate_count": campaign_count,
        "summary_campaign_candidate_count": _safe_int(
            summary.get("campaign_candidate_count")
        ),
        "product_candidate_count": product_count,
        "summary_product_candidate_count": _safe_int(
            summary.get("product_candidate_count")
        ),
        "candidate_count": campaign_count + product_count,
        "summary_candidate_count": _safe_int(summary.get("candidate_count")),
        "source_keys": sorted(
            (
                item["platform_code"],
                item["source_key"],
            )
            for item in group_summaries
            if item["platform_code"] and item["source_key"]
        ),
        "campaign_candidate_keys": sorted(
            _candidate_identity(item, "campaign")
            for item in campaign_candidates
            if _candidate_identity(item, "campaign")
        ),
        "product_candidate_keys": sorted(
            _candidate_identity(item, "product")
            for item in product_candidates
            if _candidate_identity(item, "product")
        ),
        "group_summaries": group_summaries,
        "campaign_candidates": campaign_candidates,
        "product_candidates": product_candidates,
        "all_group_paths_safe": bool(
            group_summaries
            and all(
                item["receipt_path_safe"] and item["parser_artifact_path_safe"]
                for item in group_summaries
            )
        ),
        "all_handoff_statuses_safe": bool(
            group_summaries
            and all(
                item["handoff_status"] in SAFE_HANDOFF_STATUSES
                for item in group_summaries
            )
        ),
        "group_counts_match_candidates": bool(
            group_summaries
            and all(
                item["campaign_candidate_count"]
                == item["observed_campaign_candidate_count"]
                and item["product_candidate_count"]
                == item["observed_product_candidate_count"]
                and item["campaign_candidate_keys"]
                == item["observed_campaign_candidate_keys"]
                and item["product_candidate_keys"]
                == item["observed_product_candidate_keys"]
                for item in group_summaries
            )
        ),
        "summary_counts_match_candidates": bool(
            _safe_int(summary.get("source_count")) == len(group_summaries)
            and _safe_int(summary.get("campaign_candidate_count")) == campaign_count
            and _safe_int(summary.get("product_candidate_count")) == product_count
            and _safe_int(summary.get("candidate_count"))
            == campaign_count + product_count
        ),
        "raw_payload_submitted_to_api": _contains_forbidden_key(
            candidate_handoff,
            FORBIDDEN_RAW_PAYLOAD_KEYS,
        ),
        "secret_or_token_submitted_to_api": _contains_forbidden_key(
            candidate_handoff,
            FORBIDDEN_SECRET_KEYS,
            safe_keys=SAFE_SECRET_METADATA_KEYS,
        ),
        "blocked_side_effects": _blocked_side_effects(candidate_handoff),
    }


def _handoff_gates(
    *,
    parser_review_received,
    handoff_payload_received,
    handoff_valid_object,
    parser,
    handoff,
):
    parser_source_keys = set(parser["source_keys"])
    handoff_source_keys = set(handoff["source_keys"])
    parser_campaign_keys = set(parser["campaign_candidate_keys"])
    handoff_campaign_keys = set(handoff["campaign_candidate_keys"])
    parser_product_keys = set(parser["product_candidate_keys"])
    handoff_product_keys = set(handoff["product_candidate_keys"])
    policy = handoff["queue_policy"]
    operator_boundaries_confirmed = bool(
        handoff["operator_confirmed_no_database_write"]
        and handoff["operator_confirmed_no_external_network"]
        and handoff["operator_confirmed_no_scheduler_attach"]
        and handoff["operator_confirmed_no_persistence"]
        and handoff["operator_confirmed_manual_review_required"]
        and handoff["operator_confirmed_no_secret_payload"]
    )
    return [
        {
            "key": "parser_review_payload_or_result_received",
            "label": "已提供 parser review package 或已審核結果",
            "passed": parser_review_received,
        },
        {
            "key": "parser_review_accepted",
            "label": "parser review gate 必須已通過",
            "passed": parser["accepted"],
        },
        {
            "key": "parser_ready_for_candidate_handoff_review",
            "label": "parser review 只放行到 candidate handoff review",
            "passed": parser["ready_for_manual_fetch_candidate_handoff_review"],
        },
        {
            "key": "parser_side_effect_free",
            "label": "parser review 未顯示 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
            "passed": parser["side_effects_clear"],
        },
        {
            "key": "handoff_payload_received",
            "label": "已提供 candidate handoff 結構化摘要",
            "passed": handoff_payload_received,
        },
        {
            "key": "handoff_valid_object",
            "label": "candidate handoff payload 必須是 JSON object",
            "passed": handoff_valid_object,
        },
        {
            "key": "handoff_identity_recorded",
            "label": "candidate handoff 必須記錄 handoff_id",
            "passed": bool(handoff["handoff_id"]),
        },
        {
            "key": "handoff_mode_preview_only",
            "label": "candidate handoff 必須維持 operator_review_preview",
            "passed": handoff["handoff_mode"] == "operator_review_preview",
        },
        {
            "key": "handoff_sources_match_parser",
            "label": "handoff source 必須完全對齊 parser review",
            "passed": bool(parser_source_keys and handoff_source_keys == parser_source_keys),
        },
        {
            "key": "handoff_candidates_match_parser",
            "label": "handoff 候選 key 必須完全對齊 parser review",
            "passed": bool(
                parser_campaign_keys
                and parser_product_keys
                and handoff_campaign_keys == parser_campaign_keys
                and handoff_product_keys == parser_product_keys
            ),
        },
        {
            "key": "handoff_group_paths_safe",
            "label": "handoff 引用的 receipt/parser artifact path 必須安全",
            "passed": handoff["all_group_paths_safe"],
        },
        {
            "key": "handoff_status_operator_review_only",
            "label": "handoff status 只能進人工覆核，不可標成已寫入",
            "passed": handoff["all_handoff_statuses_safe"],
        },
        {
            "key": "handoff_candidate_counts_within_limit",
            "label": "handoff 候選數需維持小批次上限",
            "passed": bool(0 < handoff["candidate_count"] <= MAX_HANDOFF_CANDIDATES),
        },
        {
            "key": "handoff_candidate_counts_match_summary",
            "label": "summary / group count 必須與候選陣列數量一致",
            "passed": bool(
                handoff["group_counts_match_candidates"]
                and handoff["summary_counts_match_candidates"]
            ),
        },
        {
            "key": "handoff_queue_policy_safe",
            "label": "queue policy 只能指向 manual_candidate_review preview",
            "passed": bool(
                handoff["target_queue"] in SAFE_TARGET_QUEUES
                and policy["target_queue"] in SAFE_TARGET_QUEUES
                and policy["dedupe_strategy"] in SAFE_DEDUPE_STRATEGIES
                and 0 < policy["max_candidates_per_handoff"] <= MAX_HANDOFF_CANDIDATES
                and policy["write_mode"] == "preview_only"
            ),
        },
        {
            "key": "handoff_operator_boundaries_confirmed",
            "label": "操作員確認無寫入、無連外、無排程、無保存且需人工覆核",
            "passed": operator_boundaries_confirmed,
        },
        {
            "key": "handoff_parser_candidates_were_safe",
            "label": "parser candidate URL 與必要欄位已通過前一 gate",
            "passed": bool(
                parser["all_candidate_urls_public"]
                and parser["all_candidate_required_fields_present"]
            ),
        },
        {
            "key": "handoff_no_raw_payload",
            "label": "handoff payload 不得回貼 raw HTML/body/snapshot",
            "passed": not handoff["raw_payload_submitted_to_api"],
        },
        {
            "key": "handoff_no_secret_or_token_key",
            "label": "handoff payload 不得包含 secret、cookie、password 或 token key",
            "passed": not handoff["secret_or_token_submitted_to_api"],
        },
        {
            "key": "handoff_side_effect_free",
            "label": "handoff payload 不得要求 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
            "passed": not handoff["blocked_side_effects"],
        },
    ]


def build_mcp_fetch_candidate_handoff_review_preview(
    *,
    parser_review_package=None,
    parser_review_result=None,
    candidate_handoff=None,
    phase=None,
):
    """建立 fetch candidate handoff review；不建立 queue 或寫入。"""
    parser_review_package = _as_dict(parser_review_package)
    parser_review_result_received = bool(
        isinstance(parser_review_result, dict) and parser_review_result
    )
    handoff_valid_object = isinstance(candidate_handoff, dict) if (
        candidate_handoff is not None
    ) else True
    handoff_payload = _as_dict(candidate_handoff)
    parser_review = _parser_review_from_inputs(
        parser_review_package,
        parser_review_result,
        phase,
    )
    parser_review_received = bool(
        parser_review_package or parser_review_result_received
    )
    payload_received = bool(
        parser_review_received or handoff_payload or candidate_handoff is not None
    )
    handoff_payload_received = bool(handoff_payload)
    parser = _parser_summary(parser_review)
    handoff = _handoff_summary(handoff_payload)
    gates = _handoff_gates(
        parser_review_received=parser_review_received,
        handoff_payload_received=handoff_payload_received,
        handoff_valid_object=handoff_valid_object,
        parser=parser,
        handoff=handoff,
    )
    blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
    accepted = bool(payload_received and not blocked_reasons)

    return {
        "mode": (
            "mcp_fetch_candidate_handoff_review"
            if payload_received
            else "mcp_fetch_candidate_handoff_review_preview"
        ),
        "phase": phase,
        "candidate_handoff_payload_received": payload_received,
        "parser_review_received": parser_review_received,
        "handoff_payload_received": handoff_payload_received,
        "handoff_valid_object": handoff_valid_object,
        "parser_review_accepted": parser["accepted"],
        "mcp_fetch_candidate_handoff_review_accepted": accepted,
        "candidate_handoff_review_ready": accepted,
        "ready_for_manual_candidate_queue_review": accepted,
        "ready_for_candidate_queue_writer_preflight": False,
        "ready_for_api_database_write": False,
        "ready_for_scheduler_attach": False,
        "manual_fetch_gate_opened_by_api": False,
        "network_request_allowed": False,
        "fetch_executed": False,
        "cli_executed": False,
        "candidate_queue_created": False,
        "candidate_queue_persisted": False,
        "parser_source_count": parser["source_count"],
        "handoff_source_count": handoff["source_count"],
        "candidate_count": handoff["candidate_count"],
        "campaign_candidate_count": handoff["campaign_candidate_count"],
        "product_candidate_count": handoff["product_candidate_count"],
        "gate_count": len(gates),
        "passed_gate_count": sum(1 for gate in gates if gate["passed"]),
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "parser_review_summary": parser,
        "candidate_handoff_summary": handoff,
        "sample_candidate_handoff_package": _sample_candidate_handoff_package(),
        "next_operator_steps": [
            "candidate handoff 通過後，只代表可進人工 candidate queue review，不代表可寫 market_*",
            "candidate queue writer、DB import、scheduler attach、AI/Telegram 摘要都必須另開獨立 gate",
            "API/UI 仍不得建立 queue、不得讀 artifact、不得抓外站、不得寫 DB、不得掛 scheduler",
        ],
        "payload_persisted": False,
        "candidate_handoff_persisted": False,
        "api_executes_health_check": False,
        "api_executes_docker": False,
        "api_executes_ssh": False,
        "api_executes_cli": False,
        "api_opens_database_connection": False,
        "api_writes_database": False,
        "api_uses_external_network": False,
        "database_session_created": False,
        "database_commit_executed": False,
        "database_write_executed": False,
        "external_network_executed": False,
        "file_written": False,
        "writes_executed": False,
        "would_write_database": False,
        "scheduler_attached": False,
    }
