"""市場情報 MCP fetch candidate queue writer preflight preview。

本模組只審核 candidate queue review 後的 writer preflight 草案；
不開 DB、不建立 transaction、不執行 CLI、不寫 queue、不掛 scheduler。
"""

from services.market_intel.mcp_fetch_candidate_queue_review import (
    build_mcp_fetch_candidate_queue_review_preview,
)


TARGET_TABLE = "market_alert_review_queue"
MAX_PREFLIGHT_ITEMS = 80
SAFE_PREFLIGHT_MODES = {"candidate_queue_writer_preflight_preview"}
SAFE_WRITE_MODES = {"cli_only_later"}
SAFE_DEDUPE_STRATEGIES = {"candidate_type_platform_source_key"}
REQUIRED_INSERT_COLUMNS = (
    "candidate_type",
    "platform_code",
    "source_key",
    "candidate_key",
    "candidate_name",
    "candidate_url",
    "review_state",
    "priority_lane",
    "evidence_ref",
)

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_queue_created",
    "candidate_queue_persisted",
    "candidate_review_state_updated",
    "cli_executed",
    "command_executed",
    "database_commit_executed",
    "database_session_created",
    "database_write_executed",
    "external_network_executed",
    "file_written",
    "network_request_allowed",
    "payload_persisted",
    "preflight_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 _safe_text(value, limit=500):
    if value is None:
        return None
    text = str(value).strip()
    return text[:limit] if text else None


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 _queue_review_from_inputs(queue_review_package, queue_review_result, phase):
    if isinstance(queue_review_result, dict) and queue_review_result:
        return queue_review_result

    queue_review_package = _as_dict(queue_review_package)
    return build_mcp_fetch_candidate_queue_review_preview(
        handoff_review_package=(
            queue_review_package.get("handoff_review_package")
            or queue_review_package.get("candidate_handoff_review")
            or queue_review_package.get("handoff_review")
        ),
        handoff_review_result=(
            queue_review_package.get("handoff_review_result")
            or queue_review_package.get("mcp_fetch_candidate_handoff_review")
        ),
        candidate_queue_review=(
            queue_review_package.get("candidate_queue_review")
            or queue_review_package.get("queue_review")
            or queue_review_package.get("review_payload")
        ),
        phase=phase,
    )


def _sample_writer_preflight_package():
    queue_preview = build_mcp_fetch_candidate_queue_review_preview()
    queue_review_package = queue_preview["sample_candidate_queue_review_package"]
    queue_review_result = build_mcp_fetch_candidate_queue_review_preview(
        handoff_review_package=queue_review_package["handoff_review_package"],
        handoff_review_result=queue_review_package["handoff_review_result"],
        candidate_queue_review=queue_review_package["candidate_queue_review"],
    )
    review_summary = queue_review_result["candidate_queue_review_summary"]
    items = review_summary.get("review_items", [])
    payload_rows = []
    for item in items:
        payload_rows.append(
            {
                "candidate_type": item.get("candidate_type"),
                "platform_code": item.get("platform_code"),
                "source_key": item.get("source_key"),
                "candidate_key": item.get("candidate_key"),
                "candidate_name": item.get("candidate_name"),
                "candidate_url": item.get("candidate_url"),
                "review_state": item.get("review_state"),
                "priority_lane": item.get("priority_lane"),
                "evidence_ref": item.get("evidence_ref"),
                "dedupe_key": (
                    f"{item.get('candidate_type')}::"
                    f"{item.get('platform_code')}::"
                    f"{item.get('source_key')}::"
                    f"{item.get('candidate_key')}"
                ),
            }
        )

    return {
        "queue_review_package": queue_review_package,
        "queue_review_result": queue_review_result,
        "writer_preflight": {
            "preflight_id": "market-intel-candidate-writer-preflight-sample",
            "preflight_mode": "candidate_queue_writer_preflight_preview",
            "target_table": TARGET_TABLE,
            "write_mode": "cli_only_later",
            "dedupe_strategy": "candidate_type_platform_source_key",
            "insert_columns": list(REQUIRED_INSERT_COLUMNS),
            "operator_confirmed_no_database_write": True,
            "operator_confirmed_no_external_network": True,
            "operator_confirmed_no_scheduler_attach": True,
            "operator_confirmed_no_cli_execution": True,
            "operator_confirmed_no_persistence": True,
            "operator_confirmed_no_secret_payload": True,
            "summary": {
                "payload_row_count": len(payload_rows),
                "campaign_candidate_count": review_summary.get(
                    "campaign_candidate_count",
                    0,
                ),
                "product_candidate_count": review_summary.get(
                    "product_candidate_count",
                    0,
                ),
            },
            "payload_rows": payload_rows,
        },
    }


def _queue_review_summary(queue_review):
    queue_review = _as_dict(queue_review)
    side_effects_clear = bool(
        not queue_review.get("network_request_allowed")
        and not queue_review.get("api_executes_cli")
        and not queue_review.get("api_opens_database_connection")
        and not queue_review.get("api_writes_database")
        and not queue_review.get("api_uses_external_network")
        and not queue_review.get("database_write_executed")
        and not queue_review.get("fetch_executed")
        and not queue_review.get("cli_executed")
        and not queue_review.get("file_written")
        and not queue_review.get("scheduler_attached")
        and not queue_review.get("candidate_queue_created")
        and not queue_review.get("candidate_queue_persisted")
        and not queue_review.get("candidate_review_state_updated")
    )
    summary = _as_dict(queue_review.get("candidate_queue_review_summary"))
    return {
        "mode": queue_review.get("mode"),
        "accepted": bool(
            queue_review.get("mcp_fetch_candidate_queue_review_accepted")
        ),
        "ready_for_candidate_queue_writer_preflight": bool(
            queue_review.get("ready_for_candidate_queue_writer_preflight")
        ),
        "review_item_count": _safe_int(queue_review.get("review_item_count")),
        "campaign_candidate_count": _safe_int(
            queue_review.get("campaign_candidate_count")
        ),
        "product_candidate_count": _safe_int(
            queue_review.get("product_candidate_count")
        ),
        "campaign_candidate_keys": sorted(
            key for key in _as_list(summary.get("campaign_candidate_keys")) if key
        ),
        "product_candidate_keys": sorted(
            key for key in _as_list(summary.get("product_candidate_keys")) if key
        ),
        "side_effects_clear": side_effects_clear,
        "blocked_reasons": queue_review.get("blocked_reasons", []),
    }


def _payload_row_summary(row):
    row = _as_dict(row)
    return {
        "candidate_type": _safe_text(row.get("candidate_type"), 40),
        "platform_code": _safe_text(row.get("platform_code"), 80),
        "source_key": _safe_text(row.get("source_key"), 160),
        "candidate_key": _safe_text(row.get("candidate_key"), 240),
        "candidate_name": _safe_text(row.get("candidate_name"), 300),
        "candidate_url": _safe_text(row.get("candidate_url"), 500),
        "review_state": _safe_text(row.get("review_state"), 80),
        "priority_lane": _safe_text(row.get("priority_lane"), 80),
        "evidence_ref": _safe_text(row.get("evidence_ref"), 160),
        "dedupe_key": _safe_text(row.get("dedupe_key"), 500),
        "required_fields_present": bool(
            row.get("candidate_type") in {"campaign", "product"}
            and row.get("platform_code")
            and row.get("source_key")
            and row.get("candidate_key")
            and row.get("candidate_name")
            and row.get("candidate_url")
            and row.get("review_state") == "needs_review"
            and row.get("dedupe_key")
        ),
    }


def _preflight_summary(writer_preflight):
    writer_preflight = _as_dict(writer_preflight)
    rows = [_payload_row_summary(row) for row in _as_list(
        writer_preflight.get("payload_rows")
    )]
    campaign_rows = [row for row in rows if row["candidate_type"] == "campaign"]
    product_rows = [row for row in rows if row["candidate_type"] == "product"]
    summary = _as_dict(writer_preflight.get("summary"))
    insert_columns = sorted(
        column for column in _as_list(writer_preflight.get("insert_columns")) if column
    )
    return {
        "provided_keys": sorted(writer_preflight.keys()),
        "preflight_id": _safe_text(writer_preflight.get("preflight_id"), 160),
        "preflight_mode": _safe_text(writer_preflight.get("preflight_mode"), 120),
        "target_table": _safe_text(writer_preflight.get("target_table"), 160),
        "write_mode": _safe_text(writer_preflight.get("write_mode"), 80),
        "dedupe_strategy": _safe_text(writer_preflight.get("dedupe_strategy"), 160),
        "insert_columns": insert_columns,
        "operator_confirmed_no_database_write": bool(
            writer_preflight.get("operator_confirmed_no_database_write")
        ),
        "operator_confirmed_no_external_network": bool(
            writer_preflight.get("operator_confirmed_no_external_network")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            writer_preflight.get("operator_confirmed_no_scheduler_attach")
        ),
        "operator_confirmed_no_cli_execution": bool(
            writer_preflight.get("operator_confirmed_no_cli_execution")
        ),
        "operator_confirmed_no_persistence": bool(
            writer_preflight.get("operator_confirmed_no_persistence")
        ),
        "operator_confirmed_no_secret_payload": bool(
            writer_preflight.get("operator_confirmed_no_secret_payload")
        ),
        "payload_row_count": len(rows),
        "summary_payload_row_count": _safe_int(summary.get("payload_row_count")),
        "campaign_candidate_count": len(campaign_rows),
        "summary_campaign_candidate_count": _safe_int(
            summary.get("campaign_candidate_count")
        ),
        "product_candidate_count": len(product_rows),
        "summary_product_candidate_count": _safe_int(
            summary.get("product_candidate_count")
        ),
        "campaign_candidate_keys": sorted(
            row["candidate_key"] for row in campaign_rows if row["candidate_key"]
        ),
        "product_candidate_keys": sorted(
            row["candidate_key"] for row in product_rows if row["candidate_key"]
        ),
        "payload_rows": rows,
        "insert_columns_cover_required": bool(
            set(REQUIRED_INSERT_COLUMNS).issubset(set(insert_columns))
        ),
        "all_rows_required_fields_present": bool(
            rows and all(row["required_fields_present"] for row in rows)
        ),
        "dedupe_keys_unique": bool(
            rows
            and len({row["dedupe_key"] for row in rows if row["dedupe_key"]})
            == len(rows)
        ),
        "summary_counts_match_rows": bool(
            _safe_int(summary.get("payload_row_count")) == len(rows)
            and _safe_int(summary.get("campaign_candidate_count")) == len(campaign_rows)
            and _safe_int(summary.get("product_candidate_count")) == len(product_rows)
        ),
        "raw_payload_submitted_to_api": _contains_forbidden_key(
            writer_preflight,
            FORBIDDEN_RAW_PAYLOAD_KEYS,
        ),
        "secret_or_token_submitted_to_api": _contains_forbidden_key(
            writer_preflight,
            FORBIDDEN_SECRET_KEYS,
            safe_keys=SAFE_SECRET_METADATA_KEYS,
        ),
        "blocked_side_effects": _blocked_side_effects(writer_preflight),
    }


def _preflight_gates(
    *,
    queue_review_received,
    writer_preflight_received,
    writer_preflight_valid_object,
    queue_review,
    preflight,
):
    queue_campaign_keys = set(queue_review["campaign_candidate_keys"])
    preflight_campaign_keys = set(preflight["campaign_candidate_keys"])
    queue_product_keys = set(queue_review["product_candidate_keys"])
    preflight_product_keys = set(preflight["product_candidate_keys"])
    operator_boundaries_confirmed = bool(
        preflight["operator_confirmed_no_database_write"]
        and preflight["operator_confirmed_no_external_network"]
        and preflight["operator_confirmed_no_scheduler_attach"]
        and preflight["operator_confirmed_no_cli_execution"]
        and preflight["operator_confirmed_no_persistence"]
        and preflight["operator_confirmed_no_secret_payload"]
    )
    return [
        {
            "key": "queue_review_payload_or_result_received",
            "label": "已提供 candidate queue review package 或已審核結果",
            "passed": queue_review_received,
        },
        {
            "key": "queue_review_accepted",
            "label": "candidate queue review gate 必須已通過",
            "passed": queue_review["accepted"],
        },
        {
            "key": "queue_review_ready_for_writer_preflight",
            "label": "queue review 只放行到 writer preflight",
            "passed": queue_review["ready_for_candidate_queue_writer_preflight"],
        },
        {
            "key": "queue_review_side_effect_free",
            "label": "queue review 未顯示 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
            "passed": queue_review["side_effects_clear"],
        },
        {
            "key": "writer_preflight_payload_received",
            "label": "已提供 writer preflight 草案",
            "passed": writer_preflight_received,
        },
        {
            "key": "writer_preflight_valid_object",
            "label": "writer preflight payload 必須是 JSON object",
            "passed": writer_preflight_valid_object,
        },
        {
            "key": "writer_preflight_identity_recorded",
            "label": "writer preflight 必須記錄 preflight_id",
            "passed": bool(preflight["preflight_id"]),
        },
        {
            "key": "writer_preflight_mode_preview_only",
            "label": "writer preflight 必須維持 preview mode",
            "passed": preflight["preflight_mode"] in SAFE_PREFLIGHT_MODES,
        },
        {
            "key": "writer_preflight_target_table_safe",
            "label": "target table 必須是 market_alert_review_queue",
            "passed": preflight["target_table"] == TARGET_TABLE,
        },
        {
            "key": "writer_preflight_write_mode_cli_later",
            "label": "write mode 必須是 cli_only_later，不得由 API 寫入",
            "passed": preflight["write_mode"] in SAFE_WRITE_MODES,
        },
        {
            "key": "writer_preflight_dedupe_strategy_safe",
            "label": "dedupe strategy 必須可由候選 identity 決定",
            "passed": preflight["dedupe_strategy"] in SAFE_DEDUPE_STRATEGIES,
        },
        {
            "key": "writer_preflight_candidates_match_queue_review",
            "label": "writer payload candidate key 必須完全對齊 queue review",
            "passed": bool(
                queue_campaign_keys
                and queue_product_keys
                and preflight_campaign_keys == queue_campaign_keys
                and preflight_product_keys == queue_product_keys
            ),
        },
        {
            "key": "writer_preflight_payload_count_within_limit",
            "label": "writer payload rows 需維持小批次上限",
            "passed": bool(0 < preflight["payload_row_count"] <= MAX_PREFLIGHT_ITEMS),
        },
        {
            "key": "writer_preflight_counts_match_summary",
            "label": "summary count 必須與 payload rows 數量一致",
            "passed": preflight["summary_counts_match_rows"],
        },
        {
            "key": "writer_preflight_required_columns_present",
            "label": "insert columns 必須覆蓋 queue 基本欄位",
            "passed": preflight["insert_columns_cover_required"],
        },
        {
            "key": "writer_preflight_required_fields_present",
            "label": "payload rows 必須具備候選識別與 needs_review 狀態",
            "passed": preflight["all_rows_required_fields_present"],
        },
        {
            "key": "writer_preflight_dedupe_keys_unique",
            "label": "payload rows dedupe key 必須唯一",
            "passed": preflight["dedupe_keys_unique"],
        },
        {
            "key": "writer_preflight_operator_boundaries_confirmed",
            "label": "操作員確認無寫入、無連外、無 CLI、無排程、無保存",
            "passed": operator_boundaries_confirmed,
        },
        {
            "key": "writer_preflight_no_raw_payload",
            "label": "writer preflight payload 不得回貼 raw HTML/body/snapshot",
            "passed": not preflight["raw_payload_submitted_to_api"],
        },
        {
            "key": "writer_preflight_no_secret_or_token_key",
            "label": "writer preflight payload 不得包含 secret、cookie、password 或 token key",
            "passed": not preflight["secret_or_token_submitted_to_api"],
        },
        {
            "key": "writer_preflight_side_effect_free",
            "label": "writer preflight payload 不得要求 API 執行、連外、寫檔、寫 DB 或掛 scheduler",
            "passed": not preflight["blocked_side_effects"],
        },
    ]


def build_mcp_fetch_candidate_queue_writer_preflight_preview(
    *,
    queue_review_package=None,
    queue_review_result=None,
    writer_preflight=None,
    phase=None,
):
    """建立 fetch candidate queue writer preflight；不開 DB 或寫入。"""
    queue_review_package = _as_dict(queue_review_package)
    queue_review_result_received = bool(
        isinstance(queue_review_result, dict) and queue_review_result
    )
    writer_preflight_valid_object = isinstance(writer_preflight, dict) if (
        writer_preflight is not None
    ) else True
    writer_preflight_payload = _as_dict(writer_preflight)
    queue_review_data = _queue_review_from_inputs(
        queue_review_package,
        queue_review_result,
        phase,
    )
    queue_review_received = bool(
        queue_review_package or queue_review_result_received
    )
    payload_received = bool(
        queue_review_received
        or writer_preflight_payload
        or writer_preflight is not None
    )
    writer_preflight_received = bool(writer_preflight_payload)
    queue_review = _queue_review_summary(queue_review_data)
    preflight = _preflight_summary(writer_preflight_payload)
    gates = _preflight_gates(
        queue_review_received=queue_review_received,
        writer_preflight_received=writer_preflight_received,
        writer_preflight_valid_object=writer_preflight_valid_object,
        queue_review=queue_review,
        preflight=preflight,
    )
    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_queue_writer_preflight"
            if payload_received
            else "mcp_fetch_candidate_queue_writer_preflight_preview"
        ),
        "phase": phase,
        "writer_preflight_payload_received": payload_received,
        "queue_review_received": queue_review_received,
        "writer_preflight_received": writer_preflight_received,
        "writer_preflight_valid_object": writer_preflight_valid_object,
        "queue_review_accepted": queue_review["accepted"],
        "mcp_fetch_candidate_queue_writer_preflight_accepted": accepted,
        "candidate_queue_writer_preflight_ready": accepted,
        "ready_for_candidate_queue_writer_cli_review": accepted,
        "ready_for_api_database_write": False,
        "ready_for_scheduler_attach": False,
        "network_request_allowed": False,
        "cli_executed": False,
        "candidate_queue_created": False,
        "candidate_queue_persisted": False,
        "candidate_review_state_updated": False,
        "review_item_count": queue_review["review_item_count"],
        "payload_row_count": preflight["payload_row_count"],
        "campaign_candidate_count": preflight["campaign_candidate_count"],
        "product_candidate_count": preflight["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,
        "queue_review_summary": queue_review,
        "writer_preflight_summary": preflight,
        "sample_writer_preflight_package": _sample_writer_preflight_package(),
        "next_operator_steps": [
            "writer preflight 通過後，只代表可進 CLI writer review，不代表可寫 market_*",
            "實際 writer run、DB commit、post-write smoke、scheduler attach 都必須另開獨立 gate",
            "API/UI 仍不得開 DB、不得執行 CLI、不得建立 queue、不得更新 review_state",
        ],
        "payload_persisted": False,
        "preflight_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,
    }
