"""市場情報 MCP fetch candidate queue writer run package review preview。

本模組只審核 writer CLI review 後的 operator run package 草案；API/UI
不建立 artifact、不寫檔、不執行 CLI、不讀 approval token、不開 DB、不寫
queue、不掛 scheduler。
"""

from services.market_intel.mcp_fetch_candidate_queue_writer_cli_review import (
    build_mcp_fetch_candidate_queue_writer_cli_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_preflight import TARGET_TABLE


MAX_RUN_PACKAGE_ITEMS = 80
SAFE_PACKAGE_MODES = {"candidate_queue_writer_run_package_review_preview"}
SAFE_PACKAGE_STAGES = {"operator_review_only"}

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

SAFE_SECRET_METADATA_KEYS = {
    "approval_token_shell_only_required",
    "contains_approval_token",
    "no_approval_token_payload",
    "no_secret_payload",
    "operator_confirmed_no_approval_token_in_payload",
    "reads_approval_token",
    "real_write_approval_token_not_included",
}

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_cli_execution",
    "allow_database_write",
    "allow_external_network_in_api",
    "allow_scheduler_attach",
    "api_executed_cli",
    "api_executes",
    "api_executes_cli",
    "api_opens_database_connection",
    "api_reads_approval_token",
    "api_uses_external_network",
    "api_writes_database",
    "api_writes_file",
    "apply_real_write",
    "attach_scheduler",
    "candidate_queue_created",
    "candidate_queue_persisted",
    "candidate_review_state_updated",
    "cli_executed",
    "command_executed",
    "contains_approval_token",
    "database_commit_executed",
    "database_connection_opened",
    "database_session_created",
    "database_write_executed",
    "execute_requested",
    "executes_now",
    "external_network_executed",
    "file_written",
    "file_written_by_api",
    "network_request_allowed",
    "opens_database_connection",
    "package_artifact_created",
    "payload_persisted",
    "ready_for_api_database_write",
    "ready_for_real_write",
    "reads_approval_token",
    "review_state_update_executed",
    "run_package_persisted",
    "scheduler_attach",
    "scheduler_attached",
    "write_database",
    "writes_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 _cli_review_from_inputs(cli_review_package, cli_review_result, phase):
    if isinstance(cli_review_result, dict) and cli_review_result:
        return cli_review_result

    cli_review_package = _as_dict(cli_review_package)
    return build_mcp_fetch_candidate_queue_writer_cli_review_preview(
        writer_preflight_package=(
            cli_review_package.get("writer_preflight_package")
            or cli_review_package.get("candidate_queue_writer_preflight")
            or cli_review_package.get("writer_preflight")
            or cli_review_package.get("preflight_package")
        ),
        writer_preflight_result=(
            cli_review_package.get("writer_preflight_result")
            or cli_review_package.get("mcp_fetch_candidate_queue_writer_preflight")
        ),
        writer_cli_review=(
            cli_review_package.get("writer_cli_review")
            or cli_review_package.get("candidate_queue_writer_cli_review")
            or cli_review_package.get("cli_review")
            or cli_review_package.get("review_payload")
        ),
        phase=phase,
    )


def _cli_review_summary(cli_review):
    cli_review = _as_dict(cli_review)
    summary = _as_dict(cli_review.get("writer_cli_review_summary"))
    side_effects_clear = bool(
        not cli_review.get("network_request_allowed")
        and not cli_review.get("api_executes_cli")
        and not cli_review.get("api_reads_approval_token")
        and not cli_review.get("api_opens_database_connection")
        and not cli_review.get("api_writes_database")
        and not cli_review.get("api_uses_external_network")
        and not cli_review.get("database_write_executed")
        and not cli_review.get("cli_executed")
        and not cli_review.get("file_written")
        and not cli_review.get("scheduler_attached")
        and not cli_review.get("candidate_queue_created")
        and not cli_review.get("candidate_queue_persisted")
        and not cli_review.get("candidate_review_state_updated")
    )
    return {
        "mode": cli_review.get("mode"),
        "accepted": bool(
            cli_review.get("mcp_fetch_candidate_queue_writer_cli_review_accepted")
        ),
        "ready_for_run_package_review": bool(
            cli_review.get("ready_for_candidate_queue_writer_run_package_review")
        ),
        "review_id": _safe_text(summary.get("review_id"), 160),
        "target_table": _safe_text(summary.get("target_table"), 160),
        "payload_row_count": _safe_int(summary.get("expected_payload_row_count")),
        "candidate_keys": sorted(
            key for key in _as_list(summary.get("expected_candidate_keys")) if key
        ),
        "dedupe_keys": sorted(
            key for key in _as_list(summary.get("expected_dedupe_keys")) if key
        ),
        "side_effects_clear": side_effects_clear,
        "blocked_reasons": cli_review.get("blocked_reasons", []),
    }


def _sample_run_package_review_package():
    cli_preview = build_mcp_fetch_candidate_queue_writer_cli_review_preview()
    cli_package = cli_preview["sample_writer_cli_review_package"]
    cli_result = build_mcp_fetch_candidate_queue_writer_cli_review_preview(
        writer_preflight_package=cli_package["writer_preflight_package"],
        writer_preflight_result=cli_package["writer_preflight_result"],
        writer_cli_review=cli_package["writer_cli_review"],
    )
    cli_summary = _cli_review_summary(cli_result)
    run_package_review = {
        "run_package_id": "market-intel-candidate-writer-run-package-sample",
        "package_mode": "candidate_queue_writer_run_package_review_preview",
        "package_stage": "operator_review_only",
        "target_table": TARGET_TABLE,
        "writer_cli_review_id": cli_summary["review_id"],
        "expected_payload_row_count": cli_summary["payload_row_count"],
        "expected_candidate_keys": cli_summary["candidate_keys"],
        "expected_dedupe_keys": cli_summary["dedupe_keys"],
        "required_artifacts": [
            {
                "key": "operator_reviewed_json",
                "path_shape": "artifacts/market_intel/reviewed-sample-*.json",
                "created_by_api": False,
            },
            {
                "key": "project_backup_zip",
                "path_shape": "scripts/tools/backups/momo_pro_system_backup_*.zip",
                "created_by_api": False,
            },
            {
                "key": "read_only_preflight_output",
                "path_shape": "artifacts/market_intel/preflight-*.json",
                "created_by_api": False,
            },
        ],
        "command_bundle": [
            {
                "step": 1,
                "key": "save_reviewed_json",
                "operator_shell_only": True,
                "api_executes": False,
                "executes_now": False,
                "writes_database": False,
                "reads_approval_token": False,
                "contains_approval_token": False,
                "opens_database_connection": False,
            },
            {
                "step": 2,
                "key": "backup_required",
                "operator_shell_only": True,
                "api_executes": False,
                "executes_now": False,
                "writes_database": False,
                "reads_approval_token": False,
                "contains_approval_token": False,
                "opens_database_connection": False,
            },
            {
                "step": 3,
                "key": "read_only_preflight",
                "operator_shell_only": True,
                "api_executes": False,
                "executes_now": False,
                "writes_database": False,
                "reads_approval_token": False,
                "contains_approval_token": False,
                "opens_database_connection": False,
            },
        ],
        "operator_confirmations": {
            "no_api_file_write": True,
            "no_api_cli_execution": True,
            "no_approval_token_payload": True,
            "no_database_write": True,
            "no_external_network": True,
            "no_scheduler_attach": True,
            "no_persistence": True,
            "no_secret_payload": True,
        },
        "promotion_policy": {
            "next_gate": "candidate_queue_writer_run_readiness_review",
            "approval_token_shell_only_required": True,
            "real_write_approval_token_not_included": True,
            "real_write_requires_separate_gate": True,
        },
        "api_execution_allowed": False,
        "real_write_allowed": False,
    }
    return {
        "writer_cli_review_package": cli_package,
        "writer_cli_review_result": cli_result,
        "writer_run_package_review": run_package_review,
    }


def _run_package_summary(run_package):
    run_package = _as_dict(run_package)
    artifacts = [
        item for item in _as_list(run_package.get("required_artifacts"))
        if isinstance(item, dict)
    ]
    commands = [
        item for item in _as_list(run_package.get("command_bundle"))
        if isinstance(item, dict)
    ]
    confirmations = _as_dict(run_package.get("operator_confirmations"))
    candidate_keys = sorted(
        key for key in _as_list(run_package.get("expected_candidate_keys")) if key
    )
    dedupe_keys = sorted(
        key for key in _as_list(run_package.get("expected_dedupe_keys")) if key
    )
    artifacts_operator_created = bool(
        artifacts
        and all(
            not item.get("created_by_api")
            and not item.get("api_writes_file")
            and not item.get("file_written_by_api")
            for item in artifacts
        )
    )
    command_bundle_api_safe = bool(
        commands
        and all(
            not item.get("api_executes")
            and not item.get("executes_now")
            and not item.get("writes_database")
            and not item.get("reads_approval_token")
            and not item.get("contains_approval_token")
            and not item.get("opens_database_connection")
            and not item.get("external_network")
            and not item.get("scheduler_attach")
            and not item.get("file_written_by_api")
            for item in commands
        )
    )
    operator_boundaries_confirmed = bool(
        confirmations.get("no_api_file_write")
        and confirmations.get("no_api_cli_execution")
        and confirmations.get("no_approval_token_payload")
        and confirmations.get("no_database_write")
        and confirmations.get("no_external_network")
        and confirmations.get("no_scheduler_attach")
        and confirmations.get("no_persistence")
        and confirmations.get("no_secret_payload")
    )
    return {
        "provided_keys": sorted(run_package.keys()),
        "run_package_id": _safe_text(
            run_package.get("run_package_id") or run_package.get("package_id"),
            160,
        ),
        "package_mode": _safe_text(run_package.get("package_mode"), 160),
        "package_stage": _safe_text(run_package.get("package_stage"), 160),
        "target_table": _safe_text(run_package.get("target_table"), 160),
        "writer_cli_review_id": _safe_text(
            run_package.get("writer_cli_review_id"), 160
        ),
        "expected_payload_row_count": _safe_int(
            run_package.get("expected_payload_row_count")
        ),
        "expected_candidate_keys": candidate_keys,
        "expected_dedupe_keys": dedupe_keys,
        "candidate_keys_unique": bool(
            candidate_keys and len(set(candidate_keys)) == len(candidate_keys)
        ),
        "dedupe_keys_unique": bool(
            dedupe_keys and len(set(dedupe_keys)) == len(dedupe_keys)
        ),
        "required_artifact_count": len(artifacts),
        "command_count": len(commands),
        "artifacts_operator_created": artifacts_operator_created,
        "command_bundle_api_safe": command_bundle_api_safe,
        "operator_boundaries_confirmed": operator_boundaries_confirmed,
        "api_execution_allowed": bool(run_package.get("api_execution_allowed")),
        "real_write_allowed": bool(run_package.get("real_write_allowed")),
        "promotion_next_gate": _safe_text(
            _as_dict(run_package.get("promotion_policy")).get("next_gate"), 160
        ),
        "raw_payload_submitted_to_api": _contains_forbidden_key(
            run_package,
            FORBIDDEN_RAW_PAYLOAD_KEYS,
        ),
        "secret_or_token_submitted_to_api": _contains_forbidden_key(
            run_package,
            FORBIDDEN_SECRET_KEYS,
            safe_keys=SAFE_SECRET_METADATA_KEYS,
        ),
        "blocked_side_effects": _blocked_side_effects(run_package),
        "required_artifacts": artifacts,
        "command_bundle": commands,
    }


def _review_gates(
    *,
    cli_received,
    run_package_received,
    run_package_valid_object,
    cli_review,
    run_package,
):
    cli_candidate_keys = set(cli_review["candidate_keys"])
    package_candidate_keys = set(run_package["expected_candidate_keys"])
    cli_dedupe_keys = set(cli_review["dedupe_keys"])
    package_dedupe_keys = set(run_package["expected_dedupe_keys"])
    return [
        {
            "key": "writer_cli_review_payload_or_result_received",
            "label": "已提供 writer CLI review package 或已審核結果",
            "passed": cli_received,
        },
        {
            "key": "writer_cli_review_accepted",
            "label": "writer CLI review gate 必須已通過",
            "passed": cli_review["accepted"],
        },
        {
            "key": "writer_cli_review_ready_for_run_package_review",
            "label": "writer CLI review 只放行到 run package review",
            "passed": cli_review["ready_for_run_package_review"],
        },
        {
            "key": "writer_cli_review_side_effect_free",
            "label": "writer CLI review 未顯示 API 執行、寫 DB 或掛 scheduler",
            "passed": cli_review["side_effects_clear"],
        },
        {
            "key": "writer_run_package_review_payload_received",
            "label": "已提供 writer run package review 草案",
            "passed": run_package_received,
        },
        {
            "key": "writer_run_package_review_valid_object",
            "label": "writer run package review payload 必須是 JSON object",
            "passed": run_package_valid_object,
        },
        {
            "key": "writer_run_package_identity_recorded",
            "label": "run package 必須記錄 run_package_id",
            "passed": bool(run_package["run_package_id"]),
        },
        {
            "key": "writer_run_package_mode_preview_only",
            "label": "run package 必須維持 review preview mode",
            "passed": run_package["package_mode"] in SAFE_PACKAGE_MODES,
        },
        {
            "key": "writer_run_package_stage_operator_review_only",
            "label": "run package stage 必須是 operator_review_only",
            "passed": run_package["package_stage"] in SAFE_PACKAGE_STAGES,
        },
        {
            "key": "writer_run_package_target_table_safe",
            "label": "target table 必須是 market_alert_review_queue",
            "passed": run_package["target_table"] == TARGET_TABLE,
        },
        {
            "key": "writer_run_package_cli_review_id_matches",
            "label": "run package 必須對齊 writer CLI review id",
            "passed": bool(
                run_package["writer_cli_review_id"]
                and run_package["writer_cli_review_id"] == cli_review["review_id"]
            ),
        },
        {
            "key": "writer_run_package_payload_count_matches_cli_review",
            "label": "run package row count 必須對齊 CLI review",
            "passed": bool(
                0 < run_package["expected_payload_row_count"] <= MAX_RUN_PACKAGE_ITEMS
                and run_package["expected_payload_row_count"]
                == cli_review["payload_row_count"]
            ),
        },
        {
            "key": "writer_run_package_candidate_keys_match_cli_review",
            "label": "run package candidate keys 必須完全對齊 CLI review",
            "passed": bool(
                cli_candidate_keys
                and package_candidate_keys == cli_candidate_keys
                and run_package["candidate_keys_unique"]
            ),
        },
        {
            "key": "writer_run_package_dedupe_keys_match_cli_review",
            "label": "run package dedupe keys 必須完全對齊 CLI review",
            "passed": bool(
                cli_dedupe_keys
                and package_dedupe_keys == cli_dedupe_keys
                and run_package["dedupe_keys_unique"]
            ),
        },
        {
            "key": "writer_run_package_artifacts_operator_created",
            "label": "required artifacts 必須由操作員建立，API 不產檔",
            "passed": run_package["artifacts_operator_created"],
        },
        {
            "key": "writer_run_package_command_bundle_api_safe",
            "label": "command bundle 只能描述人工 shell 步驟，API 不執行",
            "passed": run_package["command_bundle_api_safe"],
        },
        {
            "key": "writer_run_package_operator_boundaries_confirmed",
            "label": "操作員確認 API 不寫檔、不執行 CLI、不讀 token、不寫 DB",
            "passed": run_package["operator_boundaries_confirmed"],
        },
        {
            "key": "writer_run_package_no_api_execution_or_real_write",
            "label": "run package review payload 不得允許 API execution 或 real write",
            "passed": bool(
                not run_package["api_execution_allowed"]
                and not run_package["real_write_allowed"]
            ),
        },
        {
            "key": "writer_run_package_no_raw_payload",
            "label": "run package review payload 不得回貼 raw HTML/body/snapshot",
            "passed": not run_package["raw_payload_submitted_to_api"],
        },
        {
            "key": "writer_run_package_no_secret_or_token_key",
            "label": "run package review payload 不得包含 secret、cookie、password 或 token key",
            "passed": not run_package["secret_or_token_submitted_to_api"],
        },
        {
            "key": "writer_run_package_side_effect_free",
            "label": "run package review payload 不得要求 API 寫檔、執行、寫 DB 或掛 scheduler",
            "passed": not run_package["blocked_side_effects"],
        },
    ]


def build_mcp_fetch_candidate_queue_writer_run_package_review_preview(
    *,
    writer_cli_review_package=None,
    writer_cli_review_result=None,
    writer_run_package_review=None,
    phase=None,
):
    """建立 fetch candidate queue writer run package review；不執行任何寫入。"""
    writer_cli_review_package = _as_dict(writer_cli_review_package)
    cli_result_received = bool(
        isinstance(writer_cli_review_result, dict) and writer_cli_review_result
    )
    run_package_valid_object = (
        isinstance(writer_run_package_review, dict)
        if writer_run_package_review is not None
        else True
    )
    run_package_payload = _as_dict(writer_run_package_review)
    cli_data = _cli_review_from_inputs(
        writer_cli_review_package,
        writer_cli_review_result,
        phase,
    )
    cli_received = bool(writer_cli_review_package or cli_result_received)
    payload_received = bool(
        cli_received or run_package_payload or writer_run_package_review is not None
    )
    run_package_received = bool(run_package_payload)
    cli_review = _cli_review_summary(cli_data)
    run_package = _run_package_summary(run_package_payload)
    gates = _review_gates(
        cli_received=cli_received,
        run_package_received=run_package_received,
        run_package_valid_object=run_package_valid_object,
        cli_review=cli_review,
        run_package=run_package,
    )
    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_run_package_review"
            if payload_received
            else "mcp_fetch_candidate_queue_writer_run_package_review_preview"
        ),
        "phase": phase,
        "writer_run_package_review_payload_received": payload_received,
        "writer_cli_review_received": cli_received,
        "writer_run_package_review_received": run_package_received,
        "writer_run_package_review_valid_object": run_package_valid_object,
        "writer_cli_review_accepted": cli_review["accepted"],
        "mcp_fetch_candidate_queue_writer_run_package_review_accepted": accepted,
        "candidate_queue_writer_run_package_review_ready": accepted,
        "ready_for_candidate_queue_writer_run_readiness_review": accepted,
        "ready_for_api_database_write": False,
        "ready_for_real_write": False,
        "ready_for_scheduler_attach": False,
        "network_request_allowed": False,
        "api_executes_cli": False,
        "api_reads_approval_token": False,
        "api_opens_database_connection": False,
        "api_writes_database": False,
        "api_uses_external_network": False,
        "payload_row_count": run_package["expected_payload_row_count"],
        "gate_count": len(gates),
        "passed_gate_count": sum(1 for gate in gates if gate["passed"]),
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "writer_cli_review_summary": cli_review,
        "writer_run_package_review_summary": run_package,
        "sample_writer_run_package_review_package": _sample_run_package_review_package(),
        "next_operator_steps": [
            "Run package review 通過後，只代表可進入 run readiness review",
            "任何 --execute、--apply-real-write 與 approval token 仍必須留在後續人工 shell gate",
            "API/UI 仍不得建立 artifact、寫檔、執行 CLI、讀 token、開 DB 或寫 queue",
        ],
        "payload_persisted": False,
        "run_package_review_persisted": False,
        "run_package_persisted": False,
        "package_artifact_created": False,
        "api_writes_file": False,
        "database_session_created": False,
        "database_commit_executed": False,
        "database_write_executed": False,
        "external_network_executed": False,
        "cli_executed": False,
        "file_written": False,
        "writes_executed": False,
        "would_write_database": False,
        "scheduler_attached": False,
        "candidate_queue_created": False,
        "candidate_queue_persisted": False,
        "candidate_review_state_updated": False,
    }
