#!/usr/bin/env python3
"""Standalone verifier for an Evolution 82 evidence bundle.

Imports no CAIN code.  Dependencies: Python standard library + `cryptography` (Ed25519).
It checks file hashes, the Merkle root, the detached signature, chain semantics, and recomputes
headline numbers from the raw rows it ships.
"""
from __future__ import annotations

import argparse
import base64
import hashlib
import json
import sys
from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey

GENESIS = "0" * 64


def canonical(obj: Any) -> bytes:
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()


def sha256_file(path: Path) -> str:
    h = hashlib.sha256()
    with path.open("rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def merkle(leaves: Sequence[str]) -> str:
    if not leaves:
        return GENESIS
    level = [bytes.fromhex(x) for x in leaves]
    while len(level) > 1:
        nxt = []
        for i in range(0, len(level), 2):
            if i + 1 < len(level):
                nxt.append(hashlib.sha256(b"\x01" + level[i] + level[i + 1]).digest())
            else:
                nxt.append(level[i])
        level = nxt
    return level[0].hex()


def read_jsonl(path: Path) -> List[Dict[str, Any]]:
    out = []
    for line in path.read_text().splitlines():
        if line.strip():
            out.append(json.loads(line))
    return out


def row_hash(row: Mapping[str, Any]) -> str:
    return hashlib.sha256(canonical({k: v for k, v in row.items() if k != "hash"})).hexdigest()


def validate_chain(rows: Sequence[Mapping[str, Any]]) -> Dict[str, Any]:
    errors = []
    by_hash = {}
    children = {}
    prevs = []
    for i, r in enumerate(rows):
        if int(r.get("seq", -1)) != i:
            errors.append({"type": "SEQ", "index": i})
        if row_hash(r) != r.get("hash"):
            errors.append({"type": "HASH", "index": i})
        if r.get("hash") in by_hash:
            errors.append({"type": "DUP_HASH", "index": i})
        by_hash[r.get("hash")] = i
        children.setdefault(r.get("prev"), []).append(i)
        prevs.append(r.get("prev"))
    for i, r in enumerate(rows):
        prev = r.get("prev")
        if prev == GENESIS:
            continue
        j = by_hash.get(prev)
        if j != i - 1:
            errors.append({"type": "PREV", "index": i})
    forks = [p for p, kids in children.items() if len(kids) > 1]
    return {"valid": not errors, "linear": not forks, "errors": errors[:20],
            "forks": len(forks), "rows": len(rows)}


def verify(bundle: Path, *, pinned_key_b64: Optional[str] = None,
           expected_commit: Optional[str] = None) -> Dict[str, Any]:
    bundle = Path(bundle)
    checks: List[Dict[str, Any]] = []

    def add(name: str, ok: bool, detail: Any = "") -> None:
        checks.append({"check": name, "ok": bool(ok), "detail": detail})

    mpath = bundle / "MANIFEST.json"
    spath = bundle / "MANIFEST.sig"
    if not mpath.is_file() or not spath.is_file():
        return {"ok": False, "checks": [{"check": "manifest_present", "ok": False}],
                "error": "MANIFEST_MISSING"}
    manifest = json.loads(mpath.read_text())
    sig = base64.b64decode(spath.read_text())
    pub_b64 = manifest.get("signer_public_key_b64", "")
    if pinned_key_b64:
        add("pinned_key_matches", pub_b64 == pinned_key_b64, pub_b64[:16])
    try:
        Ed25519PublicKey.from_public_bytes(base64.b64decode(pub_b64)).verify(
            sig, canonical({k: v for k, v in manifest.items()}))
        add("manifest_signature", True)
    except (InvalidSignature, ValueError, TypeError) as exc:
        add("manifest_signature", False, f"{type(exc).__name__}")

    entries = manifest.get("files", [])
    add("manifest_has_files", len(entries) > 0, len(entries))
    listed = {e["path"]: e for e in entries}
    missing = [p for p in listed if not (bundle / p).is_file()]
    add("files_present", not missing, missing[:10])
    bad_hash = []
    for rel, e in listed.items():
        p = bundle / rel
        if p.is_file() and sha256_file(p) != e.get("sha256"):
            bad_hash.append(rel)
    add("file_hashes", not bad_hash, bad_hash[:10])
    on_disk = set()
    for p in bundle.rglob("*"):
        if p.is_file():
            rel = str(p.relative_to(bundle))
            if rel not in ("MANIFEST.json", "MANIFEST.sig"):
                on_disk.add(rel)
    add("no_unlisted_files", on_disk <= set(listed), sorted(on_disk - set(listed))[:10])
    recomputed = merkle([e["sha256"] for e in sorted(entries, key=lambda x: x["path"])])
    add("merkle_root", recomputed == manifest.get("merkle_root"), recomputed)

    source = manifest.get("source", {})
    add("source_commit_present", bool(source.get("commit")), source.get("commit", "")[:12])
    if expected_commit:
        add("expected_commit", source.get("commit") == expected_commit, source.get("commit"))
    add("e81_baseline_pinned", source.get("e81_baseline") ==
        "43420d3be6776a926956b85433b2b070f46c83fd", source.get("e81_baseline"))

    # Chain files: every JSONL with seq/prev/hash is validated in full.
    chain_files = []
    for rel in sorted(listed):
        p = bundle / rel
        if not p.is_file() or not rel.endswith(".jsonl"):
            continue
        try:
            rows = read_jsonl(p)
        except Exception:
            continue
        if rows and all(k in rows[0] for k in ("seq", "prev", "hash")):
            chain_files.append((rel, validate_chain(rows)))
    for rel, rep in chain_files:
        # The historical E81 gate audit is evidence of a known fork, not expected to be linear.
        if "baseline" in rel and "gate-audit" in rel:
            add(f"historical_fork_preserved:{rel}", rep["valid"] is False and rep["forks"] >= 1, rep)
        else:
            add(f"chain:{rel}", rep["valid"] and rep["linear"], rep)

    # ---- recompute headline numbers from raw rows -------------------------
    def load(rel: str) -> Any:
        p = bundle / rel
        return json.loads(p.read_text()) if p.is_file() else {}

    bench = load("benchmark/benchmark.json")
    brows_path = bundle / "benchmark/benchmark_rows.jsonl"
    if brows_path.is_file() and bench:
        brows = read_jsonl(brows_path)
        for model, rec in (bench.get("by_model") or {}).items():
            rows = [r for r in brows if r.get("model_key") == model and r.get("scored", True)]
            n = len(rows)
            k = sum(1 for r in rows if r.get("correct"))
            acc = round(k / n, 6) if n else None
            add(f"benchmark_accuracy:{model}", acc == rec.get("accuracy") and n == rec.get("n"),
                {"recomputed": acc, "n": n, "recorded": rec.get("accuracy")})
        add("benchmark_call_count", sum(1 for r in brows if r.get("calls")) ==
            bench.get("accounting", {}).get("model_call_count"),
            {"rows": len(brows), "recorded": bench.get("accounting", {}).get("model_call_count")})
        add("benchmark_no_remote", bench.get("real_remote_calls") == 0,
            bench.get("real_remote_calls"))

    collab = load("collaboration/collaboration.json")
    cpath = bundle / "collaboration/collaboration_rows.jsonl"
    if cpath.is_file() and collab:
        crows = read_jsonl(cpath)
        for arm, rec in (collab.get("arm_stats") or {}).items():
            ar = [r for r in crows if r.get("arm") == arm]
            n, k = len(ar), sum(1 for r in ar if r.get("correct"))
            acc = round(k / n, 6) if n else None
            add(f"collab_accuracy:{arm}", acc == rec.get("accuracy") and n == rec.get("n"),
                {"recomputed": acc, "n": n, "recorded": rec.get("accuracy")})

    findings = load("findings_closure.json")
    if findings:
        add("f01_unique_invocations", findings.get("F-01", {}).get("unique_invocations") == 1589,
            findings.get("F-01", {}).get("unique_invocations"))
        add("f01_no_remote", findings.get("F-01", {}).get("real_remote_responses") == 0,
            findings.get("F-01", {}).get("real_remote_responses"))
        h = findings.get("F-02", {}).get("historical_gate_audit", {})
        add("f02_hist_fork_detected", h.get("rows") == 2094 and h.get("linear") is False,
            {"rows": h.get("rows"), "linear": h.get("linear")})
        add("f03_calls_700", findings.get("F-03", {}).get("collaboration_rows", {}).get("model_call_count") == 700,
            findings.get("F-03", {}).get("collaboration_rows", {}).get("model_call_count"))
        add("f04_post_hoc_flagged", "CANNOT be shown" in str(findings.get("F-04", {}).get("claim", "")),
            findings.get("F-04", {}).get("claim"))

    cap = load("capacity/capacity.json")
    if cap:
        for s in cap.get("stages", []):
            if s.get("status") == "PASS":
                add(f"capacity_stage:{s.get('workers')}",
                    int(s.get("workers_ok", 0)) == int(s.get("workers", -1))
                    and int(s.get("calls", 0)) == int(s.get("configured_calls", -2)),
                    {"workers": s.get("workers"), "calls": s.get("calls"),
                     "simultaneous": s.get("actual_simultaneous_inference")})
        add("capacity_no_fake_simultaneous",
            all(int(s.get("actual_simultaneous_inference", 0)) <= int(s.get("workers", 0))
                for s in cap.get("stages", []) if s.get("status") == "PASS"),
            [s.get("actual_simultaneous_inference") for s in cap.get("stages", [])])

    xh = load("crosshost/crosshost.json")
    if xh:
        add("crosshost_event_prefix", xh.get("event_prefix_preserved") is True)
        add("crosshost_checkpoint_prefix", xh.get("checkpoint_prefix_preserved") is True)
        add("crosshost_no_duplicates", xh.get("duplicate_effects") == 0,
            xh.get("duplicate_effects"))
        add("crosshost_state_completed", xh.get("mission_state_on_b") == "COMPLETED",
            xh.get("mission_state_on_b"))
        add("crosshost_lost_ack", xh.get("lost_acknowledgement", {}).get("ok") is True)

    gates = load("acceptance_matrix.json")
    if gates:
        gs = gates.get("gates", [])
        add("gates_present", len(gs) >= 40, len(gs))
        allowed = {"PASS", "PARTIAL", "FAIL", "BLOCKED", "NOT EVALUABLE"}
        add("gates_statuses_valid", all(g.get("status") in allowed for g in gs),
            [g.get("gate") for g in gs if g.get("status") not in allowed][:10])
        add("gates_have_evidence", all(g.get("evidence_source") for g in gs),
            [g.get("gate") for g in gs if not g.get("evidence_source")][:10])
        counts = {}
        for g in gs:
            counts[g["status"]] = counts.get(g["status"], 0) + 1
        add("gates_counts_match", counts == gates.get("counts"), {"recomputed": counts,
                                                                  "recorded": gates.get("counts")})
        # A remote gate must never be PASS with zero remote calls.
        remote = [g for g in gs if g.get("gate") == "G11"]
        if remote:
            add("remote_gate_not_false_pass", not (remote[0].get("status") == "PASS" and
                                                   load("benchmark/benchmark.json").get("real_remote_calls") == 0),
                remote[0].get("status"))

    add("key_class_stated", manifest.get("key_class") == "SELF_GENERATED_OPERATOR_KEY")
    add("independent_witness_false", manifest.get("independent_witness") is False)
    ok = all(c["ok"] for c in checks)
    return {"schema": "cain2027.e82.offline_verification.v1", "ok": ok,
            "checks_passed": sum(1 for c in checks if c["ok"]), "checks_total": len(checks),
            "checks": checks, "merkle_root": manifest.get("merkle_root"),
            "file_count": manifest.get("file_count"),
            "signer_public_key_b64": pub_b64,
            "evidence_class": "LOCAL_OFFLINE",
            "independent_witness": False,
            "limits": ["self-generated signing key", "no third-party audit",
                       "same project produced and verified the evidence"]}


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("bundle", nargs="?", default="artifacts/e82/bundle")
    ap.add_argument("--pinned-key", default=None, help="base64 Ed25519 public key")
    ap.add_argument("--expected-commit", default=None)
    ap.add_argument("--json", default=None, help="write the report here")
    args = ap.parse_args()
    report = verify(Path(args.bundle), pinned_key_b64=args.pinned_key,
                    expected_commit=args.expected_commit)
    text = json.dumps(report, indent=1, sort_keys=True)
    if args.json:
        Path(args.json).write_text(text)
    print(text)
    return 0 if report["ok"] else 1


if __name__ == "__main__":
    raise SystemExit(main())
