"""
AI Agent runtime verifier evidence review snapshot.

Loads the latest committed P2-403I runtime verifier evidence implementation
review. This module never implements or executes the verifier, reads canonical
targets, writes rollback work items, sends Telegram receipts, or starts a
runtime worker.
"""

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_verifier_evidence_review_*.json"
_SCHEMA_VERSION = "ai_agent_runtime_verifier_evidence_review_v1"


def load_latest_ai_agent_runtime_verifier_evidence_review(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed AI Agent runtime verifier evidence review."""
    directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
    candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
    if not candidates:
        raise FileNotFoundError(
            f"no AI Agent runtime verifier evidence review 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_runtime_boundaries(payload, str(latest))
    _require_review_contract(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_verifier_evidence_review_only_no_live_execution":
        raise ValueError(
            f"{label}: runtime_authority must remain runtime_verifier_evidence_review_only_no_live_execution"
        )


def _require_runtime_boundaries(payload: dict[str, Any], label: str) -> None:
    boundaries = payload.get("approval_boundaries") or {}
    enabled = sorted(key for key, value in boundaries.items() if value is not False)
    if enabled:
        raise ValueError(f"{label}: approval boundaries must remain false: {enabled}")

    truth = payload.get("review_truth") or {}
    false_flags = {
        "runtime_verifier_implementation_allowed",
        "post_write_verifier_execution_allowed",
        "canonical_readback_allowed",
    }
    unsafe = sorted(flag for flag in false_flags if truth.get(flag) is not False)
    if unsafe:
        raise ValueError(f"{label}: verifier review runtime flags must remain false: {unsafe}")

    zero_counts = {
        "runtime_verifier_executed_count",
        "canonical_readback_executed_count",
        "rollback_work_item_created_count",
        "telegram_failure_receipt_sent_count",
        "learning_writeback_after_verifier_count",
    }
    non_zero = sorted(key for key in zero_counts if truth.get(key) != 0)
    if non_zero:
        raise ValueError(f"{label}: verifier review counts must remain zero: {non_zero}")


def _require_review_contract(payload: dict[str, Any], label: str) -> None:
    package = payload.get("review_package") or {}
    required_evidence = set(package.get("required_evidence") or [])
    required_minimum = {
        "approved_write_event_id",
        "dry_run_preview_hash",
        "target_write_surface",
        "canonical_readback_query_plan",
        "rollback_work_item_template",
        "failure_receipt_template",
        "redacted_evidence_refs",
    }
    missing = sorted(required_minimum - required_evidence)
    if missing:
        raise ValueError(f"{label}: runtime verifier review missing required evidence: {missing}")

    if not payload.get("evidence_checks"):
        raise ValueError(f"{label}: evidence checks must not be empty")
    if not payload.get("implementation_review_lanes"):
        raise ValueError(f"{label}: implementation review lanes must not be empty")
    if not payload.get("operator_actions"):
        raise ValueError(f"{label}: operator actions must not be empty")

    redaction = payload.get("display_redaction_contract") or {}
    if redaction.get("redaction_required") is not True:
        raise ValueError(f"{label}: frontend redaction must be required")
    for flag in ("raw_payload_display_allowed", "private_reasoning_display_allowed", "secret_value_display_allowed"):
        if redaction.get(flag) is not False:
            raise ValueError(f"{label}: {flag} must remain false")


def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
    rollups = payload.get("rollups") or {}
    checks = payload.get("evidence_checks") or []
    lanes = payload.get("implementation_review_lanes") or []
    actions = payload.get("operator_actions") or []
    package = payload.get("review_package") or {}
    expected_counts = {
        "evidence_check_count": len(checks),
        "implementation_review_lane_count": len(lanes),
        "operator_action_count": len(actions),
        "blocked_runtime_action_count": len({
            *(check.get("blocked_runtime_action") for check in checks),
            *(lane.get("blocked_runtime_action") for lane in lanes),
            *(action.get("blocked_runtime_action") for action in actions),
        }),
        "required_evidence_count": len(package.get("required_evidence") or []),
        "forbidden_evidence_count": len(package.get("forbidden_evidence") or []),
    }
    mismatched = {
        key: {"expected": expected, "actual": rollups.get(key)}
        for key, expected in expected_counts.items()
        if rollups.get(key) != expected
    }
    if mismatched:
        raise ValueError(f"{label}: rollup counts must match payload sections: {mismatched}")

    approval_required = sorted(
        action.get("action_id")
        for action in actions
        if action.get("status") == "approval_required"
    )
    if sorted(rollups.get("approval_required_action_ids") or []) != approval_required:
        raise ValueError(f"{label}: rollups.approval_required_action_ids mismatch")
    if rollups.get("live_verifier_execution_count") != 0:
        raise ValueError(f"{label}: live verifier execution count must remain zero")
