"""
AI Agent runtime readback fixture approval snapshot.

Loads the latest committed P2-112 fixture-only runtime readback approval package.
This module validates committed evidence only; it never reads canonical runtime
targets, performs live queries, executes runtime readback, writes Gateway queues,
sends Telegram messages, calls Bot API, writes receipts/result captures, reads
secrets, or performs 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_fixture_approval_*.json"
_SCHEMA_VERSION = "ai_agent_runtime_readback_fixture_approval_v1"
_RUNTIME_AUTHORITY = "runtime_readback_fixture_approval_only_no_canonical_target_or_live_query"


def load_latest_ai_agent_runtime_readback_fixture_approval(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed runtime readback fixture 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 fixture approval 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")

    label = str(latest)
    _require_schema(payload, label)
    _require_prior(payload, label)
    _require_truth(payload, label)
    _require_cards(payload, label)
    _require_contracts(payload, label)
    _require_checks(payload, label)
    _require_blockers(payload, label)
    _require_actions(payload, label)
    _require_display_redaction(payload, label)
    _require_no_forbidden_display_terms(payload, label)
    _require_rollup_consistency(payload, label)
    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 {}
    expected = {
        "current_priority": "P2",
        "current_task_id": "P2-112",
        "next_task_id": "P2-113",
        "read_only_mode": True,
        "runtime_authority": _RUNTIME_AUTHORITY,
        "overall_completion_percent": 100,
    }
    mismatches = _mismatches(status, expected)
    if mismatches:
        raise ValueError(f"{label}: program_status mismatch: {mismatches}")
    if not status.get("status_note"):
        raise ValueError(f"{label}: program_status.status_note is required")


def _require_prior(payload: dict[str, Any], label: str) -> None:
    runtime = payload.get("prior_runtime_review") or {}
    runtime_expected = {
        "implementation_review_schema_version": "ai_agent_runtime_readback_implementation_review_v1",
        "implementation_review_card_count": 5,
        "no_write_verifier_check_count": 5,
        "implementation_blocker_count": 5,
        "runtime_readback_execution_count": 0,
        "live_query_count": 0,
        "production_write_count": 0,
    }
    runtime_mismatches = _mismatches(runtime, runtime_expected)
    if runtime_mismatches:
        raise ValueError(f"{label}: prior_runtime_review mismatch: {runtime_mismatches}")
    if not runtime.get("readiness_note"):
        raise ValueError(f"{label}: prior_runtime_review.readiness_note is required")

    delivery = payload.get("prior_delivery_approval") or {}
    delivery_expected = {
        "delivery_approval_schema_version": "ai_agent_report_live_delivery_approval_package_v1",
        "delivery_approval_packet_count": 5,
        "route_lock_gate_count": 4,
        "payload_redaction_check_count": 5,
        "dry_run_delivery_receipt_count": 4,
        "telegram_send_count": 0,
        "gateway_queue_write_count": 0,
        "bot_api_call_count": 0,
    }
    delivery_mismatches = _mismatches(delivery, delivery_expected)
    if delivery_mismatches:
        raise ValueError(f"{label}: prior_delivery_approval mismatch: {delivery_mismatches}")
    if not delivery.get("readiness_note"):
        raise ValueError(f"{label}: prior_delivery_approval.readiness_note is required")


def _require_truth(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("fixture_approval_truth") or {}
    required_true = {
        "p2_110_implementation_review_loaded",
        "p2_111_delivery_approval_loaded",
        "fixture_approval_package_ready",
        "adapter_contract_ready",
        "verifier_fixture_ready",
        "blocker_mapping_ready",
        "owner_review_required_before_readback",
    }
    missing = sorted(field for field in required_true if truth.get(field) is not True)
    if missing:
        raise ValueError(f"{label}: fixture approval ready flags must remain true: {missing}")

    required_false = {
        "canonical_runtime_target_read_enabled",
        "live_query_enabled",
        "runtime_readback_execution_enabled",
        "gateway_queue_write_enabled",
        "telegram_send_enabled",
        "bot_api_call_enabled",
        "report_receipt_write_enabled",
        "result_capture_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}: live read/send/write flags must remain false: {unsafe}")

    zero_counts = {
        "owner_approval_received_count",
        "fixture_readback_execution_count",
        "canonical_runtime_target_read_count",
        "live_query_count",
        "gateway_queue_write_count",
        "telegram_send_count",
        "bot_api_call_count",
        "report_receipt_write_count",
        "result_capture_write_count",
        "production_write_count",
    }
    non_zero = sorted(field for field in zero_counts if truth.get(field) != 0)
    if non_zero:
        raise ValueError(f"{label}: fixture approval live counters must remain zero: {non_zero}")
    if not truth.get("truth_note"):
        raise ValueError(f"{label}: fixture_approval_truth.truth_note is required")


def _require_cards(payload: dict[str, Any], label: str) -> None:
    cards = payload.get("fixture_approval_cards") or []
    required = {
        "report_delivery_fixture_readback",
        "runtime_implementation_fixture_readback",
        "telegram_failure_receipt_fixture_readback",
        "reviewer_queue_fixture_preview",
        "result_capture_fixture_link",
    }
    card_ids = {card.get("card_id") for card in cards}
    if card_ids != required:
        raise ValueError(f"{label}: fixture approval cards must match {sorted(required)}")

    for card in cards:
        card_id = card.get("card_id")
        if card.get("owner_approval_required") is not True or card.get("fixture_only") is not True:
            raise ValueError(f"{label}: card {card_id} must require owner approval and fixture_only")
        if card.get("status") not in {"approval_required", "ready_for_owner_review", "blocked_by_policy"}:
            raise ValueError(f"{label}: card {card_id} status is invalid")
        if card.get("risk_tier") not in {"medium", "high", "critical"}:
            raise ValueError(f"{label}: card {card_id} risk_tier is invalid")
        if not card.get("required_fixture_fields") or not card.get("blocked_runtime_actions"):
            raise ValueError(f"{label}: card {card_id} must list fixture fields and blocked actions")
        if not _is_redacted_sha256(card.get("evidence_hash")):
            raise ValueError(f"{label}: card {card_id} must expose redacted evidence_hash")


def _require_contracts(payload: dict[str, Any], label: str) -> None:
    contracts = payload.get("adapter_contracts") or []
    required = {
        "report_delivery_payload_to_fixture_readback",
        "implementation_review_to_adapter_check",
        "failure_receipt_to_fixture_verifier",
        "result_capture_to_fixture_promotion",
    }
    contract_ids = {contract.get("contract_id") for contract in contracts}
    if contract_ids != required:
        raise ValueError(f"{label}: adapter contracts must match {sorted(required)}")

    for contract in contracts:
        contract_id = contract.get("contract_id")
        if contract.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: contract {contract_id} status is invalid")
        if not contract.get("input_schema") or not contract.get("output_schema"):
            raise ValueError(f"{label}: contract {contract_id} input/output schema is required")
        if contract.get("canonical_target_read_enabled") is not False or contract.get("live_query_enabled") is not False:
            raise ValueError(f"{label}: contract {contract_id} must not enable live target read/query")
        if not contract.get("required_evidence"):
            raise ValueError(f"{label}: contract {contract_id} required_evidence is required")
        if not _is_redacted_sha256(contract.get("evidence_hash")):
            raise ValueError(f"{label}: contract {contract_id} must expose redacted evidence_hash")


def _require_checks(payload: dict[str, Any], label: str) -> None:
    checks = payload.get("verifier_fixture_checks") or []
    required = {
        "fixture_payload_shape",
        "no_live_target_reference",
        "route_lock_fixture",
        "redaction_fixture",
        "result_capture_no_write_fixture",
    }
    check_ids = {check.get("check_id") for check in checks}
    if check_ids != required:
        raise ValueError(f"{label}: verifier fixture checks must match {sorted(required)}")

    for check in checks:
        check_id = check.get("check_id")
        if check.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: verifier check {check_id} status is invalid")
        if check.get("live_verifier_enabled") is not False:
            raise ValueError(f"{label}: verifier check {check_id} must not enable live verifier")
        if not check.get("required_fixture") or not check.get("failure_if_missing"):
            raise ValueError(f"{label}: verifier check {check_id} must include fixture and failure text")
        if not _is_redacted_sha256(check.get("evidence_hash")):
            raise ValueError(f"{label}: verifier check {check_id} must expose redacted evidence_hash")


def _require_blockers(payload: dict[str, Any], label: str) -> None:
    blockers = payload.get("blocker_mappings") or []
    required = {
        "canonical_runtime_target_blocked",
        "gateway_queue_write_blocked",
        "telegram_bot_send_blocked",
        "report_receipt_write_blocked",
        "result_capture_write_blocked",
    }
    blocker_ids = {blocker.get("blocker_id") for blocker in blockers}
    if blocker_ids != required:
        raise ValueError(f"{label}: blocker mappings must match {sorted(required)}")

    for blocker in blockers:
        blocker_id = blocker.get("blocker_id")
        if blocker.get("severity") not in {"medium", "high", "critical"}:
            raise ValueError(f"{label}: blocker {blocker_id} severity is invalid")
        if blocker.get("status") not in {"mapped", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: blocker {blocker_id} status is invalid")
        if not blocker.get("blocked_action") or not blocker.get("blocked_until"):
            raise ValueError(f"{label}: blocker {blocker_id} blocked action/until is required")
        if not _is_redacted_sha256(blocker.get("evidence_hash")):
            raise ValueError(f"{label}: blocker {blocker_id} must expose redacted evidence_hash")


def _require_actions(payload: dict[str, Any], label: str) -> None:
    actions = payload.get("operator_actions") or []
    required = {
        "review_fixture_approval_cards",
        "compare_adapter_contracts",
        "confirm_no_live_query",
        "reject_canonical_target_scope",
        "promote_to_p2_113_fixture_readback",
    }
    action_ids = {action.get("action_id") for action in actions}
    if action_ids != required:
        raise ValueError(f"{label}: operator actions must match {sorted(required)}")

    valid_types = {
        "review_fixture_approval",
        "compare_adapter_contract",
        "confirm_no_live_query",
        "reject_canonical_target",
        "promote_to_p2_113",
    }
    for action in actions:
        action_id = action.get("action_id")
        if action.get("action_type") not in valid_types:
            raise ValueError(f"{label}: action {action_id} action_type is invalid")
        if action.get("runtime_readback_allowed") is not False:
            raise ValueError(f"{label}: action {action_id} must not allow runtime readback")
        if not action.get("operator_instruction"):
            raise ValueError(f"{label}: action {action_id} operator_instruction is required")


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 be required")
    false_fields = {
        "raw_prompt_display_allowed",
        "private_reasoning_display_allowed",
        "secret_value_display_allowed",
        "raw_runtime_payload_display_allowed",
        "internal_collaboration_content_display_allowed",
    }
    unsafe = sorted(field for field in false_fields if contract.get(field) is not False)
    if unsafe:
        raise ValueError(f"{label}: display redaction flags must remain false: {unsafe}")
    if not contract.get("frontend_display_policy"):
        raise ValueError(f"{label}: frontend_display_policy is required")


def _require_no_forbidden_display_terms(payload: dict[str, Any], label: str) -> None:
    serialized = json.dumps(payload, ensure_ascii=False).lower()
    forbidden = {
        "work_window_transcript",
        "session_id",
        "browser_context",
        "authorization_header",
        "raw telegram payload",
        "private reasoning",
        "raw prompt",
        "chain-of-thought",
    }
    hits = sorted(term for term in forbidden if term in serialized)
    if hits:
        raise ValueError(f"{label}: forbidden display terms leaked: {hits}")


def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
    rollups = payload.get("rollups") or {}
    expected_counts = {
        "fixture_approval_card_count": len(payload.get("fixture_approval_cards") or []),
        "adapter_contract_count": len(payload.get("adapter_contracts") or []),
        "verifier_fixture_check_count": len(payload.get("verifier_fixture_checks") or []),
        "blocker_mapping_count": len(payload.get("blocker_mappings") or []),
        "operator_action_count": len(payload.get("operator_actions") or []),
        "approval_required_card_count": sum(
            1 for card in payload.get("fixture_approval_cards") or [] if card.get("status") == "approval_required"
        ),
        "blocked_card_count": sum(
            1 for card in payload.get("fixture_approval_cards") or [] if card.get("status") == "blocked_by_policy"
        ),
        "blocked_contract_count": sum(
            1 for contract in payload.get("adapter_contracts") or [] if contract.get("status") == "blocked_by_policy"
        ),
        "blocked_check_count": sum(
            1 for check in payload.get("verifier_fixture_checks") or [] if check.get("status") == "blocked_by_policy"
        ),
    }
    mismatches = _mismatches(rollups, expected_counts)
    if mismatches:
        raise ValueError(f"{label}: rollup counts mismatch: {mismatches}")

    zero_rollups = {
        "owner_approval_received_count",
        "fixture_readback_execution_count",
        "canonical_runtime_target_read_count",
        "live_query_count",
        "gateway_queue_write_count",
        "telegram_send_count",
        "bot_api_call_count",
        "report_receipt_write_count",
        "result_capture_write_count",
        "production_write_count",
        "secret_read_count",
        "destructive_operation_count",
    }
    non_zero = sorted(field for field in zero_rollups if rollups.get(field) != 0)
    if non_zero:
        raise ValueError(f"{label}: live/send/write rollups must remain zero: {non_zero}")


def _mismatches(actual: dict[str, Any], expected: dict[str, Any]) -> dict[str, dict[str, Any]]:
    return {
        key: {"expected": expected_value, "actual": actual.get(key)}
        for key, expected_value in expected.items()
        if actual.get(key) != expected_value
    }


def _is_redacted_sha256(value: Any) -> bool:
    if not isinstance(value, str):
        return False
    if not value.startswith("sha256:") or len(value) != len("sha256:") + 64:
        return False
    digest = value.split(":", 1)[1]
    return all(char in "0123456789abcdef" for char in digest)
