"""
AI Agent owner-approved result capture dry-run snapshot.

Loads the latest committed P2-106 owner-approved result capture dry-run
contract. This module validates repo-committed evidence only; it never writes
scores, result capture rows, learning state, PlayBook trust, KM, audit,
timeline, Gateway queues, Telegram messages, production targets, or secrets.
"""

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_owner_approved_result_capture_dry_run_*.json"
_SCHEMA_VERSION = "ai_agent_owner_approved_result_capture_dry_run_v1"
_RUNTIME_AUTHORITY = "owner_approved_result_capture_dry_run_only_no_live_write"


def load_latest_ai_agent_owner_approved_result_capture_dry_run(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed owner-approved result capture dry-run contract."""
    directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
    candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
    if not candidates:
        raise FileNotFoundError(
            f"no AI Agent owner-approved result capture dry-run 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_contract(payload, str(latest))
    _require_dry_run_truth(payload, str(latest))
    _require_approval_packet(payload, str(latest))
    _require_result_capture_templates(payload, str(latest))
    _require_score_fixtures(payload, str(latest))
    _require_dry_run_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-106":
        raise ValueError(f"{label}: current_task_id must be P2-106")
    if status.get("next_task_id") != "P2-107":
        raise ValueError(f"{label}: next_task_id must be P2-107")


def _require_prior_contract(payload: dict[str, Any], label: str) -> None:
    prior = payload.get("prior_contract_readback") or {}
    if prior.get("source_schema_version") != "ai_agent_critic_reviewer_result_capture_v1":
        raise ValueError(f"{label}: prior_contract_readback must chain from P2-105")
    required_counts = {
        "scorecard_count": 5,
        "result_capture_contract_count": 5,
        "promotion_gate_count": 6,
        "candidate_route_count": 4,
        "approved_without_execution_meta_24h": 63,
        "execution_failed_with_matched_24h": 1,
        "result_capture_runtime_write_count": 0,
        "learning_write_count": 0,
        "playbook_trust_write_count": 0,
        "telegram_send_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-105 prior contract counts mismatch: {mismatches}")


def _require_dry_run_truth(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("dry_run_truth") or {}
    required_true = {
        "p2_105_contract_loaded",
        "owner_approval_required",
        "dry_run_preview_allowed",
        "result_capture_payload_template_ready",
        "critic_reviewer_score_fixture_ready",
        "post_write_verifier_fixture_required",
        "redacted_operator_digest_ready",
    }
    missing = sorted(field for field in required_true if truth.get(field) is not True)
    if missing:
        raise ValueError(f"{label}: dry-run readiness flags must remain true: {missing}")

    required_false = {
        "runtime_result_capture_write_enabled",
        "runtime_score_write_enabled",
        "runtime_learning_write_enabled",
        "playbook_trust_write_enabled",
        "gateway_queue_write_enabled",
        "telegram_send_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 write/send flags must remain false: {unsafe}")

    zero_counts = {
        "owner_approval_received_count",
        "dry_run_preview_generated_count",
        "result_capture_write_count_24h",
        "score_write_count_24h",
        "learning_write_count_24h",
        "playbook_trust_write_count_24h",
        "gateway_queue_write_count_24h",
        "telegram_send_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}: dry-run live counters must remain zero: {non_zero}")


def _require_approval_packet(payload: dict[str, Any], label: str) -> None:
    packet = payload.get("approval_packet") or {}
    required_fields = set(packet.get("required_owner_fields") or [])
    required_minimum = {
        "owner_approval_id",
        "owner_role",
        "approval_scope",
        "approved_result_capture_contract_ids",
        "redacted_evidence_refs",
        "dry_run_plan_fingerprint",
        "rollback_plan_ref",
        "post_write_verifier_plan_ref",
    }
    missing = sorted(required_minimum - required_fields)
    if missing:
        raise ValueError(f"{label}: approval packet missing owner fields: {missing}")
    if not packet.get("operator_meaning"):
        raise ValueError(f"{label}: approval packet must include operator_meaning")
    if not _is_redacted_sha256(packet.get("dry_run_plan_fingerprint")):
        raise ValueError(f"{label}: approval packet must expose dry_run_plan_fingerprint")


def _require_result_capture_templates(payload: dict[str, Any], label: str) -> None:
    templates = payload.get("result_capture_dry_run_templates") or []
    template_ids = {template.get("template_id") for template in templates}
    required = {
        "dry_run_capture_approved_execution_result",
        "dry_run_capture_execution_failed_candidate",
        "dry_run_capture_pending_human_gate",
        "dry_run_capture_manual_or_noop",
        "dry_run_capture_post_write_verifier_receipt",
    }
    if template_ids != required:
        raise ValueError(f"{label}: result capture dry-run templates must match {sorted(required)}")
    valid_statuses = {"ready_for_dry_run", "approval_required", "blocked_by_policy"}
    for template in templates:
        template_id = template.get("template_id")
        if template.get("status") not in valid_statuses:
            raise ValueError(f"{label}: template {template_id} status is invalid")
        if template.get("write_enabled") is not False:
            raise ValueError(f"{label}: template {template_id} write_enabled must remain false")
        if template.get("runtime_writer_enabled") is not False:
            raise ValueError(f"{label}: template {template_id} runtime_writer_enabled must remain false")
        if not template.get("required_inputs") or not template.get("preview_outputs"):
            raise ValueError(f"{label}: template {template_id} must list required inputs and preview outputs")
        if not _is_redacted_sha256(template.get("no_write_evidence_hash")):
            raise ValueError(f"{label}: template {template_id} must expose no_write_evidence_hash")


def _require_score_fixtures(payload: dict[str, Any], label: str) -> None:
    fixtures = payload.get("critic_reviewer_score_fixtures") or []
    fixture_ids = {fixture.get("fixture_id") for fixture in fixtures}
    required = {
        "fixture_openclaw_critic_decision_quality",
        "fixture_openclaw_reviewer_safety_verdict",
        "fixture_hermes_redaction_operator_report",
        "fixture_nemotron_failure_candidate_verifier",
        "fixture_coordinator_disagreement_gate",
    }
    if fixture_ids != required:
        raise ValueError(f"{label}: critic reviewer score fixtures must match {sorted(required)}")
    for fixture in fixtures:
        fixture_id = fixture.get("fixture_id")
        if fixture.get("fixture_only") is not True:
            raise ValueError(f"{label}: fixture {fixture_id} fixture_only must remain true")
        if fixture.get("runtime_score_write_enabled") is not False:
            raise ValueError(f"{label}: fixture {fixture_id} runtime_score_write_enabled must remain false")
        if not isinstance(fixture.get("minimum_score"), int):
            raise ValueError(f"{label}: fixture {fixture_id} minimum_score must be integer")
        if not fixture.get("required_inputs") or not fixture.get("failure_if_missing"):
            raise ValueError(f"{label}: fixture {fixture_id} must list required inputs and failure text")
        if not _is_redacted_sha256(fixture.get("evidence_hash")):
            raise ValueError(f"{label}: fixture {fixture_id} must expose evidence_hash")


def _require_dry_run_gates(payload: dict[str, Any], label: str) -> None:
    gates = payload.get("dry_run_gates") or []
    gate_ids = {gate.get("gate_id") for gate in gates}
    required = {
        "gate_owner_approval_packet_complete",
        "gate_critic_reviewer_score_fixture_complete",
        "gate_result_capture_payload_preview_complete",
        "gate_redaction_public_display_safe",
        "gate_no_live_write_enforced",
        "gate_post_write_verifier_fixture_ready",
        "gate_operator_digest_preview_only",
    }
    if gate_ids != required:
        raise ValueError(f"{label}: dry-run gates must match {sorted(required)}")
    for gate in gates:
        gate_id = gate.get("gate_id")
        if gate.get("status") not in {"ready", "approval_required", "blocked_by_policy"}:
            raise ValueError(f"{label}: gate {gate_id} status is invalid")
        if gate.get("creates_runtime_write") is not False:
            raise ValueError(f"{label}: gate {gate_id} creates_runtime_write must remain false")
        if not gate.get("required_evidence") or not gate.get("blocked_write_action"):
            raise ValueError(f"{label}: gate {gate_id} must list evidence and blocked write action")


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", "collect_evidence", "approve_dry_run", "reject_or_rework", "promote_to_next_gate"}
    if not required.issubset(action_types):
        raise ValueError(f"{label}: operator actions must cover {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("dry_run_truth") or {}
    prior = payload.get("prior_contract_readback") or {}
    templates = payload.get("result_capture_dry_run_templates") or []
    fixtures = payload.get("critic_reviewer_score_fixtures") or []
    gates = payload.get("dry_run_gates") or []
    actions = payload.get("operator_actions") or []
    expected = {
        "result_capture_template_count": len(templates),
        "score_fixture_count": len(fixtures),
        "dry_run_gate_count": len(gates),
        "operator_action_count": len(actions),
        "approval_required_gate_count": sum(1 for gate in gates if gate.get("status") == "approval_required"),
        "blocked_gate_count": sum(1 for gate in gates if gate.get("status") == "blocked_by_policy"),
        "approval_24h_total": prior.get("approval_24h_total"),
        "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"),
        "dry_run_preview_generated_count": truth.get("dry_run_preview_generated_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"),
        "gateway_queue_write_count": truth.get("gateway_queue_write_count_24h"),
        "telegram_send_count": truth.get("telegram_send_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:"))
