"""
AI Agent result capture writer dry-run fixture snapshot.

Loads the latest committed P2-123 writer dry-run fixture package.
This module validates committed evidence only; it never applies writers, writes
result captures, writes learning records, updates PlayBook trust, writes
reviewer / 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_writer_dry_run_fixture_*.json"
_SCHEMA_VERSION = "ai_agent_result_capture_writer_dry_run_fixture_v1"
_RUNTIME_AUTHORITY = "result_capture_writer_dry_run_fixture_only_no_live_write"
_TARGET_DRY_RUN = "result_capture_writer_dry_run_fixture"


def load_latest_ai_agent_result_capture_writer_dry_run_fixture(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed result capture writer dry-run fixture 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 writer dry-run fixture 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_writer_dry_run_fixtures(payload, label)
    _require_receipt_previews(payload, label)
    _require_idempotency_replay(payload, label)
    _require_rollback_rehearsals(payload, label)
    _require_blocked_writes(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-123",
        "next_task_id": "P2-124",
        "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_writer_implementation_review") or {}
    expected = {
        "schema_version": "ai_agent_result_capture_writer_implementation_review_v1",
        "writer_implementation_review_count": 5,
        "implementation_gate_count": 5,
        "post_implementation_verifier_plan_count": 5,
        "blocked_runtime_change_count": 6,
        "operator_action_count": 5,
        "writer_apply_count": 0,
        "implementation_applied_count": 0,
        "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_writer_implementation_review mismatch: {mismatches}")
    if not prior.get("readiness_note"):
        raise ValueError(f"{label}: prior_result_capture_writer_implementation_review.readiness_note is required")


def _require_truth(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("dry_run_truth") or {}
    required_true = {
        "p2_122_implementation_review_loaded",
        "result_capture_writer_dry_run_fixture_ready",
        "receipt_preview_ready",
        "idempotency_replay_ready",
        "rollback_rehearsal_ready",
        "redaction_preview_ready",
        "fixture_only_mode",
        "owner_approval_required",
        "dual_approval_required",
        "dry_run_hash_required",
        "idempotency_key_required",
        "post_dry_run_verifier_required",
        "rollback_required",
    }
    missing = sorted(field for field in required_true if truth.get(field) is not True)
    if missing:
        raise ValueError(f"{label}: dry-run ready flags must remain true: {missing}")
    required_false = {
        "writer_apply_enabled",
        "dry_run_execution_enabled",
        "receipt_write_enabled",
        "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",
        "dual_approval_received_count",
        "dry_run_hash_verified_count",
        "writer_apply_count",
        "dry_run_execution_count",
        "receipt_write_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",
        "secret_read_count",
        "destructive_operation_count",
    }
    non_zero = sorted(field for field in zero_counts if truth.get(field) != 0)
    if non_zero:
        raise ValueError(f"{label}: dry-run live counters must remain zero: {non_zero}")
    if not truth.get("truth_note"):
        raise ValueError(f"{label}: dry_run_truth.truth_note is required")


def _require_writer_dry_run_fixtures(payload: dict[str, Any], label: str) -> None:
    fixtures = payload.get("writer_dry_run_fixtures") or []
    required = {
        "fixture_result_capture_writer",
        "fixture_learning_writer",
        "fixture_playbook_trust_writer",
        "fixture_reviewer_queue_writer",
        "fixture_gateway_queue_writer",
    }
    fixture_ids = {fixture.get("fixture_id") for fixture in fixtures}
    if fixture_ids != required:
        raise ValueError(f"{label}: writer dry-run fixtures must match {sorted(required)}")
    for fixture in fixtures:
        fixture_id = fixture.get("fixture_id")
        if fixture.get("target_dry_run") != _TARGET_DRY_RUN:
            raise ValueError(f"{label}: fixture {fixture_id} must target {_TARGET_DRY_RUN}")
        if fixture.get("fixture_only") is not True:
            raise ValueError(f"{label}: fixture {fixture_id} must remain fixture-only")
        if fixture.get("runtime_write_enabled") is not False:
            raise ValueError(f"{label}: fixture {fixture_id} must not enable runtime write")
        if fixture.get("status") not in {"ready_for_owner_review", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: fixture {fixture_id} status is invalid")
        for field in {"input_fixture", "expected_receipt_preview", "idempotency_key", "blocked_live_action"}:
            if not fixture.get(field):
                raise ValueError(f"{label}: fixture {fixture_id} missing {field}")
        if not _is_redacted_sha256(fixture.get("evidence_hash")):
            raise ValueError(f"{label}: fixture {fixture_id} must expose redacted evidence_hash")


def _require_receipt_previews(payload: dict[str, Any], label: str) -> None:
    previews = payload.get("receipt_previews") or []
    required = {
        "receipt_result_capture_preview",
        "receipt_learning_preview",
        "receipt_playbook_trust_preview",
        "receipt_reviewer_queue_preview",
        "receipt_gateway_queue_preview",
    }
    preview_ids = {preview.get("receipt_id") for preview in previews}
    if preview_ids != required:
        raise ValueError(f"{label}: receipt previews must match {sorted(required)}")
    for preview in previews:
        receipt_id = preview.get("receipt_id")
        if preview.get("live_receipt_enabled") is not False:
            raise ValueError(f"{label}: receipt preview {receipt_id} must not enable live receipt")
        if preview.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: receipt preview {receipt_id} status is invalid")
        for field in {"preview_payload", "redaction_policy", "failure_if_missing"}:
            if not preview.get(field):
                raise ValueError(f"{label}: receipt preview {receipt_id} missing {field}")


def _require_idempotency_replay(payload: dict[str, Any], label: str) -> None:
    checks = payload.get("idempotency_replay_checks") or []
    required = {
        "replay_duplicate_noop",
        "replay_conflict_reject",
        "replay_retry_same_receipt",
        "replay_rollback_rehearsal",
        "replay_gateway_queue_noop",
    }
    check_ids = {check.get("check_id") for check in checks}
    if check_ids != required:
        raise ValueError(f"{label}: idempotency replay checks must match {sorted(required)}")
    for check in checks:
        check_id = check.get("check_id")
        if check.get("live_execution_enabled") is not False:
            raise ValueError(f"{label}: replay check {check_id} must not enable live execution")
        if check.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: replay check {check_id} status is invalid")
        if not check.get("expected_outcome") or not check.get("failure_if_missing"):
            raise ValueError(f"{label}: replay check {check_id} must include expected outcome and failure")
        if not _is_redacted_sha256(check.get("evidence_hash")):
            raise ValueError(f"{label}: replay check {check_id} must expose redacted evidence_hash")


def _require_rollback_rehearsals(payload: dict[str, Any], label: str) -> None:
    rehearsals = payload.get("rollback_rehearsals") or []
    required = {
        "rollback_result_capture_preview",
        "rollback_learning_preview",
        "rollback_playbook_trust_preview",
        "rollback_reviewer_queue_preview",
        "rollback_gateway_queue_preview",
    }
    rehearsal_ids = {rehearsal.get("rehearsal_id") for rehearsal in rehearsals}
    if rehearsal_ids != required:
        raise ValueError(f"{label}: rollback rehearsals must match {sorted(required)}")
    for rehearsal in rehearsals:
        rehearsal_id = rehearsal.get("rehearsal_id")
        if rehearsal.get("live_execution_enabled") is not False:
            raise ValueError(f"{label}: rollback rehearsal {rehearsal_id} must not enable live execution")
        if rehearsal.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: rollback rehearsal {rehearsal_id} status is invalid")
        if not rehearsal.get("rollback_preview") or not rehearsal.get("blocked_live_action"):
            raise ValueError(f"{label}: rollback rehearsal {rehearsal_id} must include preview and blocked action")


def _require_blocked_writes(payload: dict[str, Any], label: str) -> None:
    blockers = payload.get("blocked_runtime_writes") or []
    required = {
        "blocked_result_capture_writer_apply",
        "blocked_learning_writer_apply",
        "blocked_playbook_trust_writer_apply",
        "blocked_reviewer_queue_apply",
        "blocked_gateway_queue_apply",
        "blocked_telegram_send_apply",
    }
    blocker_ids = {blocker.get("blocker_id") for blocker in blockers}
    if blocker_ids != required:
        raise ValueError(f"{label}: blocked runtime 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_writer_dry_run_fixtures",
        "verify_receipt_preview_contract",
        "replay_idempotency_fixture",
        "rehearse_rollback_preview",
        "promote_to_p2_124",
    }
    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_write_allowed") is not False:
            raise ValueError(f"{label}: action {action_id} must not allow runtime 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 {}
    fixtures = payload.get("writer_dry_run_fixtures") or []
    previews = payload.get("receipt_previews") or []
    replays = payload.get("idempotency_replay_checks") or []
    rehearsals = payload.get("rollback_rehearsals") or []
    blockers = payload.get("blocked_runtime_writes") or []
    actions = payload.get("operator_actions") or []
    expected = {
        "writer_dry_run_fixture_count": len(fixtures),
        "receipt_preview_count": len(previews),
        "idempotency_replay_check_count": len(replays),
        "rollback_rehearsal_count": len(rehearsals),
        "blocked_runtime_write_count": len(blockers),
        "operator_action_count": len(actions),
        "approval_required_fixture_count": sum(1 for item in fixtures if item.get("status") == "approval_required"),
        "blocked_fixture_count": sum(1 for item in fixtures if item.get("status") == "blocked_by_policy"),
        "approval_required_receipt_count": sum(1 for item in previews if item.get("status") == "approval_required"),
        "blocked_receipt_count": sum(1 for item in previews if item.get("status") == "blocked_by_policy"),
        "approval_required_replay_count": sum(1 for item in replays if item.get("status") == "approval_required"),
        "blocked_replay_count": sum(1 for item in replays if item.get("status") == "blocked_by_policy"),
        "approval_required_rollback_count": sum(1 for item in rehearsals 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,
        "dual_approval_received_count": 0,
        "dry_run_hash_verified_count": 0,
        "writer_apply_count": 0,
        "dry_run_execution_count": 0,
        "receipt_write_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
    }
