"""
AI Agent result capture promotion approval gate snapshot.

Loads the latest committed P2-119 result capture promotion approval package. This
module validates committed evidence only; it never writes result captures,
writes learning records, updates PlayBook trust, writes Gateway queues, sends
Telegram messages, reads canonical runtime targets, 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_result_capture_promotion_approval_gate_*.json"
_SCHEMA_VERSION = "ai_agent_result_capture_promotion_approval_gate_v1"
_RUNTIME_AUTHORITY = "result_capture_promotion_approval_gate_only_no_capture_or_learning_write"
_TARGET_PROMOTION = "result_capture_promotion_preview"


def load_latest_ai_agent_result_capture_promotion_approval_gate(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed result capture promotion approval gate package."""
    directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
    candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
    if not candidates:
        raise FileNotFoundError(f"no AI Agent result capture promotion approval gate 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_promotion_packets(payload, label)
    _require_acceptance_templates(payload, label)
    _require_verifier_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-119",
        "next_task_id": "P2-120",
        "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:
    prior = payload.get("prior_result_capture_no_write_readback") or {}
    expected = {
        "schema_version": "ai_agent_result_capture_no_write_readback_v1",
        "result_capture_readback_fixture_count": 5,
        "capture_field_mapping_count": 5,
        "readback_verifier_check_count": 5,
        "blocked_result_capture_write_count": 5,
        "operator_action_count": 5,
        "result_capture_write_count": 0,
        "learning_write_count": 0,
        "playbook_trust_write_count": 0,
        "reviewer_queue_write_count": 0,
        "gateway_queue_write_count": 0,
        "telegram_send_count": 0,
        "bot_api_call_count": 0,
        "report_receipt_write_count": 0,
    }
    mismatches = _mismatches(prior, expected)
    if mismatches:
        raise ValueError(f"{label}: prior_result_capture_no_write_readback mismatch: {mismatches}")
    if not prior.get("readiness_note"):
        raise ValueError(f"{label}: prior_result_capture_no_write_readback.readiness_note is required")


def _require_truth(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("promotion_truth") or {}
    required_true = {
        "p2_118_result_capture_readback_loaded",
        "promotion_approval_package_ready",
        "acceptance_gate_template_ready",
        "promotion_verifier_ready",
        "operator_handoff_ready",
        "rollback_and_reverify_required",
    }
    missing = sorted(field for field in required_true if truth.get(field) is not True)
    if missing:
        raise ValueError(f"{label}: promotion ready flags must remain true: {missing}")
    for field in {"owner_approval_received", "capture_promotion_approved"}:
        if truth.get(field) is not False:
            raise ValueError(f"{label}: {field} must remain false before promotion write")

    required_false = {
        "canonical_runtime_target_read_enabled",
        "live_query_enabled",
        "reviewer_queue_write_enabled",
        "gateway_queue_write_enabled",
        "telegram_send_enabled",
        "bot_api_call_enabled",
        "report_receipt_write_enabled",
        "result_capture_write_enabled",
        "learning_write_enabled",
        "playbook_trust_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",
        "capture_promotion_approved_count",
        "canonical_runtime_target_read_count",
        "live_query_count",
        "reviewer_queue_write_count",
        "gateway_queue_write_count",
        "telegram_send_count",
        "bot_api_call_count",
        "report_receipt_write_count",
        "result_capture_write_count",
        "learning_write_count",
        "playbook_trust_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}: promotion live counters must remain zero: {non_zero}")
    if not truth.get("truth_note"):
        raise ValueError(f"{label}: promotion_truth.truth_note is required")


def _require_promotion_packets(payload: dict[str, Any], label: str) -> None:
    packets = payload.get("promotion_approval_packets") or []
    required = {
        "promotion_packet_action_required",
        "promotion_packet_no_action_review",
        "promotion_packet_verifier_degraded",
        "promotion_packet_route_lock",
        "promotion_packet_owner_acceptance",
    }
    packet_ids = {packet.get("packet_id") for packet in packets}
    if packet_ids != required:
        raise ValueError(f"{label}: promotion approval packets must match {sorted(required)}")
    for packet in packets:
        packet_id = packet.get("packet_id")
        if packet.get("promotion_mode") != "approval_gate_only":
            raise ValueError(f"{label}: packet {packet_id} must remain approval_gate_only")
        if packet.get("result_capture_write_enabled") is not False:
            raise ValueError(f"{label}: packet {packet_id} must not enable result capture write")
        if packet.get("status") not in {"ready_for_owner_review", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: packet {packet_id} status is invalid")
        if not packet.get("required_decision") or not packet.get("approval_lane"):
            raise ValueError(f"{label}: packet {packet_id} must include approval lane and required decision")
        if not _is_redacted_sha256(packet.get("evidence_hash")):
            raise ValueError(f"{label}: packet {packet_id} must expose redacted evidence_hash")


def _require_acceptance_templates(payload: dict[str, Any], label: str) -> None:
    templates = payload.get("acceptance_gate_templates") or []
    required = {
        "owner_acceptance_record_template",
        "capture_write_scope_template",
        "learning_write_scope_template",
        "playbook_trust_scope_template",
        "rollback_and_verifier_template",
    }
    template_ids = {template.get("template_id") for template in templates}
    if template_ids != required:
        raise ValueError(f"{label}: acceptance gate templates must match {sorted(required)}")
    for template in templates:
        template_id = template.get("template_id")
        if template.get("target_promotion") != _TARGET_PROMOTION:
            raise ValueError(f"{label}: template {template_id} must target {_TARGET_PROMOTION}")
        if template.get("promotion_write_enabled") is not False:
            raise ValueError(f"{label}: template {template_id} must not enable promotion write")
        if template.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: template {template_id} status is invalid")
        if not template.get("required_owner") or not template.get("required_fields"):
            raise ValueError(f"{label}: template {template_id} required owner and fields are required")
        if not _is_redacted_sha256(template.get("evidence_hash")):
            raise ValueError(f"{label}: template {template_id} must expose redacted evidence_hash")


def _require_verifier_checks(payload: dict[str, Any], label: str) -> None:
    checks = payload.get("promotion_verifier_checks") or []
    required = {
        "no_result_capture_write_before_approval",
        "no_learning_write_before_approval",
        "no_playbook_trust_before_approval",
        "no_gateway_queue_before_approval",
        "promotion_redaction_completeness",
    }
    verifier_ids = {check.get("verifier_id") for check in checks}
    if verifier_ids != required:
        raise ValueError(f"{label}: promotion verifier checks must match {sorted(required)}")
    for check in checks:
        verifier_id = check.get("verifier_id")
        if check.get("live_execution_enabled") is not False:
            raise ValueError(f"{label}: verifier {verifier_id} must not enable live execution")
        if check.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: verifier {verifier_id} status is invalid")
        if not check.get("verifies") or not check.get("failure_if_missing"):
            raise ValueError(f"{label}: verifier {verifier_id} must include verifies and failure_if_missing")
        if not _is_redacted_sha256(check.get("evidence_hash")):
            raise ValueError(f"{label}: verifier {verifier_id} must expose redacted evidence_hash")


def _require_blockers(payload: dict[str, Any], label: str) -> None:
    blockers = payload.get("blocked_promotion_writes") or []
    required = {
        "result_capture_promotion_not_authorized",
        "learning_promotion_not_authorized",
        "playbook_trust_promotion_not_authorized",
        "gateway_queue_promotion_not_authorized",
        "production_promotion_not_authorized",
    }
    blocker_ids = {blocker.get("blocker_id") for blocker in blockers}
    if blocker_ids != required:
        raise ValueError(f"{label}: blocked promotion writes must match {sorted(required)}")
    for blocker in blockers:
        blocker_id = blocker.get("blocker_id")
        if blocker.get("status") not in {"approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: blocker {blocker_id} status is invalid")
        if blocker.get("severity") not in {"high", "critical"}:
            raise ValueError(f"{label}: blocker {blocker_id} severity is invalid")
        if not blocker.get("blocked_action") or not blocker.get("blocked_until"):
            raise ValueError(f"{label}: blocker {blocker_id} must include blocked_action and blocked_until")
        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_promotion_approval_packet",
        "verify_no_write_promotion_counts",
        "confirm_acceptance_scope",
        "check_promotion_redaction_contract",
        "promote_to_p2_120",
    }
    action_ids = {action.get("action_id") for action in actions}
    if action_ids != required:
        raise ValueError(f"{label}: operator actions must match {sorted(required)}")
    for action in actions:
        action_id = action.get("action_id")
        if action.get("runtime_promotion_write_allowed") is not False:
            raise ValueError(f"{label}: action {action_id} must not allow runtime promotion write")
        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 {}
    expected = {
        "redaction_required": True,
        "raw_prompt_display_allowed": False,
        "private_reasoning_display_allowed": False,
        "secret_value_display_allowed": False,
        "raw_runtime_payload_display_allowed": False,
        "internal_collaboration_content_display_allowed": False,
    }
    mismatches = _mismatches(contract, expected)
    if mismatches:
        raise ValueError(f"{label}: display_redaction_contract mismatch: {mismatches}")
    if not contract.get("frontend_display_policy"):
        raise ValueError(f"{label}: display_redaction_contract.frontend_display_policy is required")


def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
    rollups = payload.get("rollups") or {}
    packets = payload.get("promotion_approval_packets") or []
    templates = payload.get("acceptance_gate_templates") or []
    verifiers = payload.get("promotion_verifier_checks") or []
    blockers = payload.get("blocked_promotion_writes") or []
    actions = payload.get("operator_actions") or []
    expected = {
        "promotion_approval_packet_count": len(packets),
        "acceptance_gate_template_count": len(templates),
        "promotion_verifier_check_count": len(verifiers),
        "blocked_promotion_write_count": len(blockers),
        "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"),
        "approval_required_template_count": sum(1 for item in templates if item.get("status") == "approval_required"),
        "blocked_template_count": sum(1 for item in templates if item.get("status") == "blocked_by_policy"),
        "approval_required_verifier_count": sum(1 for item in verifiers if item.get("status") == "approval_required"),
        "critical_blocker_count": sum(1 for item in blockers if item.get("severity") == "critical"),
        "owner_approval_received_count": 0,
        "capture_promotion_approved_count": 0,
        "canonical_runtime_target_read_count": 0,
        "live_query_count": 0,
        "reviewer_queue_write_count": 0,
        "gateway_queue_write_count": 0,
        "telegram_send_count": 0,
        "bot_api_call_count": 0,
        "report_receipt_write_count": 0,
        "result_capture_write_count": 0,
        "learning_write_count": 0,
        "playbook_trust_write_count": 0,
        "production_write_count": 0,
        "secret_read_count": 0,
        "destructive_operation_count": 0,
    }
    mismatches = _mismatches(rollups, expected)
    if mismatches:
        raise ValueError(f"{label}: rollup counts mismatch: {mismatches}")


def _require_no_forbidden_display_terms(payload: dict[str, Any], label: str) -> None:
    serialized = json.dumps(payload, ensure_ascii=False)
    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 present: {hits}")


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


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