"""
AI Agent runtime readback approval package snapshot.

Loads the latest committed P2-109 runtime readback approval package. This
module validates repo-committed evidence only; it never reads canonical runtime
targets, writes result capture rows, writes scores, writes learning state,
updates PlayBook trust, writes reviewer/Gateway queues, sends Telegram
messages, calls Bot API, writes rollback work items, writes production targets,
reads secrets, or runs destructive operations.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any

from src.services.snapshot_paths import default_evaluations_dir

_DEFAULT_EVALUATIONS_DIR = default_evaluations_dir(Path(__file__))
_SNAPSHOT_PATTERN = "ai_agent_runtime_readback_approval_package_*.json"
_SCHEMA_VERSION = "ai_agent_runtime_readback_approval_package_v1"
_RUNTIME_AUTHORITY = "runtime_readback_approval_package_only_no_live_write"


def load_latest_ai_agent_runtime_readback_approval_package(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed runtime readback approval package."""
    directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
    candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
    if not candidates:
        raise FileNotFoundError(f"no AI Agent runtime readback approval package snapshots found in {directory}")

    latest = candidates[-1]
    with latest.open(encoding="utf-8") as handle:
        payload = json.load(handle)

    if not isinstance(payload, dict):
        raise ValueError(f"{latest}: expected JSON object")
    _require_schema(payload, str(latest))
    _require_prior_readback(payload, str(latest))
    _require_approval_truth(payload, str(latest))
    _require_approval_packets(payload, str(latest))
    _require_canonical_readback_plans(payload, str(latest))
    _require_rollback_drill_lanes(payload, str(latest))
    _require_telegram_failure_receipt_gates(payload, str(latest))
    _require_operator_actions(payload, str(latest))
    _require_display_redaction(payload, str(latest))
    _require_no_forbidden_display_terms(payload, str(latest))
    _require_rollup_consistency(payload, str(latest))
    return payload


def _require_schema(payload: dict[str, Any], label: str) -> None:
    if payload.get("schema_version") != _SCHEMA_VERSION:
        raise ValueError(f"{label}: expected schema_version={_SCHEMA_VERSION}")
    status = payload.get("program_status") or {}
    if status.get("read_only_mode") is not True:
        raise ValueError(f"{label}: program_status.read_only_mode must be true")
    if status.get("runtime_authority") != _RUNTIME_AUTHORITY:
        raise ValueError(f"{label}: runtime_authority must remain {_RUNTIME_AUTHORITY}")
    if status.get("current_task_id") != "P2-109":
        raise ValueError(f"{label}: current_task_id must be P2-109")
    if status.get("next_task_id") != "P2-110":
        raise ValueError(f"{label}: next_task_id must be P2-110")


def _require_prior_readback(payload: dict[str, Any], label: str) -> None:
    prior = payload.get("prior_readback_readiness") or {}
    if prior.get("source_schema_version") != "ai_agent_owner_approved_result_capture_readback_v1":
        raise ValueError(f"{label}: prior_readback_readiness must chain from P2-107")
    required_counts = {
        "readback_digest_count": 5,
        "promotion_review_count": 5,
        "failure_lane_count": 4,
        "reviewer_queue_preview_count": 4,
        "operator_action_count": 5,
        "approval_required_digest_count": 2,
        "blocked_total_count": 7,
        "approved_without_execution_meta_24h": 63,
        "execution_failed_with_matched_24h": 1,
        "owner_approval_received_count": 0,
        "readback_digest_generated_count": 0,
        "promotion_approved_count": 0,
        "reviewer_queue_write_count": 0,
        "result_capture_write_count": 0,
        "score_write_count": 0,
        "learning_write_count": 0,
        "playbook_trust_write_count": 0,
        "gateway_queue_write_count": 0,
        "telegram_send_count": 0,
        "production_write_count": 0,
    }
    mismatches = {
        key: {"expected": expected, "actual": prior.get(key)}
        for key, expected in required_counts.items()
        if prior.get(key) != expected
    }
    if mismatches:
        raise ValueError(f"{label}: P2-107 prior readback counts mismatch: {mismatches}")


def _require_approval_truth(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("approval_package_truth") or {}
    required_true = {
        "p2_107_readback_loaded",
        "approval_package_ready",
        "canonical_readback_plan_ready",
        "rollback_drill_ready",
        "telegram_failure_receipt_gate_ready",
        "owner_review_required_before_runtime",
    }
    missing = sorted(field for field in required_true if truth.get(field) is not True)
    if missing:
        raise ValueError(f"{label}: approval readiness flags must remain true: {missing}")

    required_false = {
        "canonical_runtime_readback_enabled",
        "runtime_result_capture_write_enabled",
        "runtime_score_write_enabled",
        "runtime_learning_write_enabled",
        "playbook_trust_write_enabled",
        "reviewer_queue_write_enabled",
        "gateway_queue_write_enabled",
        "telegram_failure_receipt_send_enabled",
        "bot_api_call_enabled",
        "rollback_work_item_write_enabled",
        "production_write_enabled",
        "secret_read_enabled",
        "destructive_operation_enabled",
    }
    unsafe = sorted(field for field in required_false if truth.get(field) is not False)
    if unsafe:
        raise ValueError(f"{label}: runtime read/write/send flags must remain false: {unsafe}")

    zero_counts = {
        "owner_approval_received_count",
        "runtime_readback_execution_count",
        "result_capture_write_count_24h",
        "score_write_count_24h",
        "learning_write_count_24h",
        "playbook_trust_write_count_24h",
        "reviewer_queue_write_count_24h",
        "gateway_queue_write_count_24h",
        "telegram_failure_receipt_send_count_24h",
        "bot_api_call_count_24h",
        "rollback_work_item_write_count_24h",
        "production_write_count_24h",
        "secret_read_count_24h",
        "destructive_operation_count_24h",
    }
    non_zero = sorted(field for field in zero_counts if truth.get(field) != 0)
    if non_zero:
        raise ValueError(f"{label}: approval package live counters must remain zero: {non_zero}")


def _require_approval_packets(payload: dict[str, Any], label: str) -> None:
    packets = payload.get("approval_packet_templates") or []
    packet_ids = {packet.get("packet_id") for packet in packets}
    required = {
        "approved_result_capture_runtime_readback",
        "execution_failed_negative_learning_review",
        "pending_human_gate_expiry_review",
        "manual_or_noop_result_acceptance",
        "post_write_verifier_receipt_package",
    }
    if packet_ids != required:
        raise ValueError(f"{label}: approval packet templates must match {sorted(required)}")

    valid_statuses = {"ready_for_owner_review", "approval_required", "blocked_by_policy"}
    valid_tiers = {"medium", "high", "critical"}
    for packet in packets:
        packet_id = packet.get("packet_id")
        if packet.get("status") not in valid_statuses:
            raise ValueError(f"{label}: packet {packet_id} status is invalid")
        if packet.get("risk_tier") not in valid_tiers:
            raise ValueError(f"{label}: packet {packet_id} risk_tier is invalid")
        if packet.get("approval_required") is not True:
            raise ValueError(f"{label}: packet {packet_id} approval_required must remain true")
        if packet.get("runtime_write_allowed") is not False:
            raise ValueError(f"{label}: packet {packet_id} runtime_write_allowed must remain false")
        if not packet.get("required_approval_fields") or not packet.get("blocked_runtime_actions"):
            raise ValueError(f"{label}: packet {packet_id} must list approval fields and blocked actions")
        if not _is_redacted_sha256(packet.get("evidence_hash")):
            raise ValueError(f"{label}: packet {packet_id} must expose evidence_hash")


def _require_canonical_readback_plans(payload: dict[str, Any], label: str) -> None:
    plans = payload.get("canonical_readback_plans") or []
    plan_ids = {plan.get("plan_id") for plan in plans}
    required = {
        "result_capture_row_readback",
        "score_fixture_parity_readback",
        "playbook_trust_noop_readback",
        "verifier_receipt_readback",
    }
    if plan_ids != required:
        raise ValueError(f"{label}: canonical readback plans must match {sorted(required)}")
    for plan in plans:
        plan_id = plan.get("plan_id")
        if plan.get("status") not in {"planned_no_runtime_read", "blocked_by_policy"}:
            raise ValueError(f"{label}: canonical readback plan {plan_id} status is invalid")
        if plan.get("live_query_enabled") is not False:
            raise ValueError(f"{label}: canonical readback plan {plan_id} live_query_enabled must remain false")
        if plan.get("runtime_write_allowed") is not False:
            raise ValueError(f"{label}: canonical readback plan {plan_id} runtime_write_allowed must remain false")
        if not plan.get("required_fields") or not plan.get("redaction_checks"):
            raise ValueError(f"{label}: canonical readback plan {plan_id} must list required fields and redaction checks")
        if not _is_redacted_sha256(plan.get("evidence_hash")):
            raise ValueError(f"{label}: canonical readback plan {plan_id} must expose evidence_hash")


def _require_rollback_drill_lanes(payload: dict[str, Any], label: str) -> None:
    lanes = payload.get("rollback_drill_lanes") or []
    lane_ids = {lane.get("lane_id") for lane in lanes}
    required = {
        "result_capture_insert_rollback_drill",
        "score_write_rollback_drill",
        "learning_write_rollback_drill",
        "telegram_receipt_rollback_drill",
    }
    if lane_ids != required:
        raise ValueError(f"{label}: rollback drill lanes must match {sorted(required)}")
    for lane in lanes:
        lane_id = lane.get("lane_id")
        if lane.get("status") not in {"planned_no_write", "blocked_by_policy"}:
            raise ValueError(f"{label}: rollback drill lane {lane_id} status is invalid")
        if lane.get("rollback_write_enabled") is not False:
            raise ValueError(f"{label}: rollback drill lane {lane_id} rollback_write_enabled must remain false")
        if lane.get("creates_runtime_write") is not False:
            raise ValueError(f"{label}: rollback drill lane {lane_id} creates_runtime_write must remain false")
        if not lane.get("rollback_owner") or not lane.get("required_steps"):
            raise ValueError(f"{label}: rollback drill lane {lane_id} must list owner and required steps")


def _require_telegram_failure_receipt_gates(payload: dict[str, Any], label: str) -> None:
    gates = payload.get("telegram_failure_receipt_gates") or []
    gate_ids = {gate.get("gate_id") for gate in gates}
    required = {
        "receipt_payload_redaction",
        "sre_war_room_route_lock",
        "delivery_correlation",
        "duplicate_suppression",
    }
    if gate_ids != required:
        raise ValueError(f"{label}: Telegram failure receipt gates must match {sorted(required)}")
    for gate in gates:
        gate_id = gate.get("gate_id")
        if gate.get("status") not in {"ready_for_owner_review", "blocked_by_policy"}:
            raise ValueError(f"{label}: Telegram failure gate {gate_id} status is invalid")
        if gate.get("queue_write_enabled") is not False:
            raise ValueError(f"{label}: Telegram failure gate {gate_id} queue_write_enabled must remain false")
        if gate.get("telegram_send_enabled") is not False:
            raise ValueError(f"{label}: Telegram failure gate {gate_id} telegram_send_enabled must remain false")
        if gate.get("bot_api_call_enabled") is not False:
            raise ValueError(f"{label}: Telegram failure gate {gate_id} bot_api_call_enabled must remain false")
        if not gate.get("required_evidence"):
            raise ValueError(f"{label}: Telegram failure gate {gate_id} must list required evidence")
        if not _is_redacted_sha256(gate.get("evidence_hash")):
            raise ValueError(f"{label}: Telegram failure gate {gate_id} must expose evidence_hash")


def _require_operator_actions(payload: dict[str, Any], label: str) -> None:
    actions = payload.get("operator_actions") or []
    action_types = {action.get("action_type") for action in actions}
    required = {
        "review_approval_package",
        "validate_canonical_plan",
        "review_rollback_drill",
        "review_failure_receipt_gate",
        "reject_or_promote",
    }
    if action_types != required:
        raise ValueError(f"{label}: operator actions must match {sorted(required)}")
    for action in actions:
        if action.get("runtime_write_allowed") is not False:
            raise ValueError(f"{label}: operator action {action.get('action_id')} must not allow runtime write")
        if not action.get("operator_instruction"):
            raise ValueError(f"{label}: operator action {action.get('action_id')} must include instruction")


def _require_display_redaction(payload: dict[str, Any], label: str) -> None:
    contract = payload.get("display_redaction_contract") or {}
    if contract.get("redaction_required") is not True:
        raise ValueError(f"{label}: display redaction must remain required")
    required_false = {
        "raw_prompt_display_allowed",
        "private_reasoning_display_allowed",
        "secret_value_display_allowed",
        "raw_telegram_payload_display_allowed",
        "work_window_transcript_display_allowed",
    }
    unsafe = sorted(field for field in required_false if contract.get(field) is not False)
    if unsafe:
        raise ValueError(f"{label}: display redaction fields must remain false: {unsafe}")


def _require_no_forbidden_display_terms(payload: dict[str, Any], label: str) -> None:
    forbidden_terms = {
        "工作視窗",
        "對話內容",
        "批准！繼續",
        "In app browser",
        "My request for Codex",
        "browser_context",
        "codex_user_message",
        "prompt_text",
        "raw prompt",
        "private reasoning",
        "chain of thought",
        "private_reasoning",
        "chain_of_thought",
        "authorization_header",
        "work window transcript",
        "internal collaboration transcript",
    }
    hits: list[str] = []

    def walk(value: Any, path: str) -> None:
        if isinstance(value, dict):
            for key, nested in value.items():
                walk(nested, f"{path}.{key}" if path else str(key))
            return
        if isinstance(value, list):
            for index, nested in enumerate(value):
                walk(nested, f"{path}[{index}]")
            return
        if isinstance(value, str):
            matched = sorted(term for term in forbidden_terms if term in value)
            if matched:
                hits.append(f"{path}: {', '.join(matched)}")

    walk(payload, "")
    if hits:
        raise ValueError(f"{label}: forbidden display terms found: {hits}")


def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
    rollups = payload.get("rollups") or {}
    truth = payload.get("approval_package_truth") or {}
    prior = payload.get("prior_readback_readiness") or {}
    packets = payload.get("approval_packet_templates") or []
    plans = payload.get("canonical_readback_plans") or []
    rollback_lanes = payload.get("rollback_drill_lanes") or []
    telegram_gates = payload.get("telegram_failure_receipt_gates") or []
    actions = payload.get("operator_actions") or []
    expected = {
        "approval_packet_count": len(packets),
        "canonical_readback_plan_count": len(plans),
        "rollback_drill_lane_count": len(rollback_lanes),
        "telegram_failure_receipt_gate_count": len(telegram_gates),
        "operator_action_count": len(actions),
        "approval_required_packet_count": sum(1 for item in packets if item.get("status") == "approval_required"),
        "blocked_packet_count": sum(1 for item in packets if item.get("status") == "blocked_by_policy"),
        "blocked_readback_plan_count": sum(1 for item in plans if item.get("status") == "blocked_by_policy"),
        "blocked_rollback_lane_count": sum(1 for item in rollback_lanes if item.get("status") == "blocked_by_policy"),
        "blocked_telegram_gate_count": sum(1 for item in telegram_gates if item.get("status") == "blocked_by_policy"),
        "approved_without_execution_meta_24h": prior.get("approved_without_execution_meta_24h"),
        "execution_failed_with_matched_24h": prior.get("execution_failed_with_matched_24h"),
        "owner_approval_received_count": truth.get("owner_approval_received_count"),
        "runtime_readback_execution_count": truth.get("runtime_readback_execution_count"),
        "result_capture_write_count": truth.get("result_capture_write_count_24h"),
        "score_write_count": truth.get("score_write_count_24h"),
        "learning_write_count": truth.get("learning_write_count_24h"),
        "playbook_trust_write_count": truth.get("playbook_trust_write_count_24h"),
        "reviewer_queue_write_count": truth.get("reviewer_queue_write_count_24h"),
        "gateway_queue_write_count": truth.get("gateway_queue_write_count_24h"),
        "telegram_failure_receipt_send_count": truth.get("telegram_failure_receipt_send_count_24h"),
        "bot_api_call_count": truth.get("bot_api_call_count_24h"),
        "rollback_work_item_write_count": truth.get("rollback_work_item_write_count_24h"),
        "production_write_count": truth.get("production_write_count_24h"),
        "secret_read_count": truth.get("secret_read_count_24h"),
        "destructive_operation_count": truth.get("destructive_operation_count_24h"),
    }
    mismatches = {
        key: {"expected": expected_value, "actual": rollups.get(key)}
        for key, expected_value in expected.items()
        if rollups.get(key) != expected_value
    }
    if mismatches:
        raise ValueError(f"{label}: rollup counts mismatch: {mismatches}")


def _is_redacted_sha256(value: Any) -> bool:
    if not isinstance(value, str):
        return False
    if not value.startswith("sha256:") or len(value) != 71:
        return False
    return all(char in "0123456789abcdef" for char in value.removeprefix("sha256:"))
