"""CAIN-A75-056 offline verifier (rules embedded verbatim). CAIN-A75-056 benchmark evidence rules. Standard library only, no CAIN imports: embedded verbatim in the offline verifier.

A benchmark score is a statement about a benchmark. It is evidence only when the task, the model, the inputs, the grader
and the result are recorded and the failures are published. A score without its grading record is marketing. The review
recomputes the package:

  tasks        every task names its input files by SHA-256 and its prompt by SHA-256, and comes from a test or held-out
               split; no task prompt appears in the declared training-data hash list
  model        the model under test is recorded with id, version and decoding settings (temperature, top_p, max_tokens)
  grader       the grader is recorded with id, kind (exact / rubric / human / model) and version; rubric and model graders
               carry a rubric hash; the grader is not the model under test
  results      exactly one result per task and nothing else; each names the recorded grader, commits its output by
               SHA-256, has a score in [0, 1], and its pass flag equals score >= threshold
  published    the published task count, pass count and mean score equal the recomputation, the failure list is every
               failing task, and the near-miss list is every failing task within the stated band below the threshold
  claims       every boolean the package asserts matches what this review computed

Verdict: SOUND (the score is backed by its grading record; it may be a low score), else UNSOUND with every failing check
named. Malformed input is UNSOUND. It reviews the record; it does not re-run the model or re-grade an output.
"""
from __future__ import annotations

import math
import re

_ID = re.compile(r"^[A-Za-z0-9_.:\-/@]{1,120}$")
_HEX = re.compile(r"^[0-9a-f]{64}$")
GRADERS = ("exact", "rubric", "human", "model")
SPLITS = ("test", "held_out")


class Bad(ValueError):
    pass


def _num(v, what, lo=0.0, hi=1e15):
    if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) or not (lo <= v <= hi):
        raise Bad(f"{what} must be a finite number in [{lo}, {hi}]")
    return float(v)


def _id(v, what):
    if not isinstance(v, str) or not _ID.match(v):
        raise Bad(f"{what} must be an id")
    return v


def _hex(v):
    return isinstance(v, str) and bool(_HEX.match(v))


def evaluate(record):
    checks, summary = [], {}

    def c(cid, ok, detail=""):
        checks.append({"id": cid, "ok": bool(ok), "detail": detail})

    try:
        if not isinstance(record, dict) or record.get("schema") != "cain42.a75.benchevidence.review.v1":
            raise Bad("not a cain42.a75.benchevidence.review.v1 record")
        threshold = _num(record.get("pass_threshold"), "pass_threshold", 0.0, 1.0)
        band = _num(record.get("near_miss_band"), "near_miss_band", 0.0, 1.0)
        training = record.get("declared_training_sha256s", [])
        if not isinstance(training, list):
            raise Bad("declared_training_sha256s must be a list")
        training = set(t for t in training if isinstance(t, str))

        model = record.get("model") if isinstance(record.get("model"), dict) else {}
        dec = model.get("decoding") if isinstance(model.get("decoding"), dict) else {}
        c("model.id", isinstance(model.get("id"), str) and bool(_ID.match(model["id"])), f"model {model.get('id')!r}")
        c("model.version", isinstance(model.get("version"), str) and bool(model["version"]), f"version {model.get('version')!r}")
        miss = [k for k in ("temperature", "top_p", "max_tokens")
                if isinstance(dec.get(k), bool) or not isinstance(dec.get(k), (int, float)) or not math.isfinite(dec[k]) or dec[k] < 0]
        c("model.decoding", not miss, f"decoding settings missing or invalid: {miss}" if miss else "temperature, top_p, max_tokens recorded")

        g = record.get("grader") if isinstance(record.get("grader"), dict) else {}
        gid, kind = g.get("id"), g.get("kind")
        c("grader.recorded", isinstance(gid, str) and bool(_ID.match(gid)) and kind in GRADERS
          and isinstance(g.get("version"), str) and bool(g["version"]), f"grader {gid!r} kind {kind!r} version {g.get('version')!r}")
        c("grader.rubric", kind in ("exact", "human") or _hex(g.get("rubric_sha256")),
          "rubric committed by SHA-256" if kind in ("rubric", "model") else f"{kind} grader needs no rubric hash")
        c("grader.independent", gid != model.get("id") and (kind != "model" or g.get("model_id") not in (None, model.get("id"))),
          f"grader {gid!r} (model {g.get('model_id')!r}) vs model under test {model.get('id')!r}")

        tasks = record.get("tasks")
        if not isinstance(tasks, list) or not tasks:
            raise Bad("tasks must be a non-empty list")
        T = {}
        for t in tasks:
            if not isinstance(t, dict):
                raise Bad("a task must be an object")
            tid = _id(t.get("id"), "task.id")
            if tid in T:
                raise Bad(f"duplicate task {tid}")
            T[tid] = t
            files = t.get("input_files")
            ok_files = isinstance(files, list) and bool(files) and all(
                isinstance(f, dict) and isinstance(f.get("name"), str) and f["name"] and _hex(f.get("sha256")) for f in files)
            c(f"task.{tid}.inputs", ok_files, f"{len(files) if isinstance(files, list) else 0} input file(s) committed by SHA-256")
            c(f"task.{tid}.prompt", _hex(t.get("prompt_sha256")), "prompt committed by SHA-256")
            c(f"task.{tid}.split", t.get("split") in SPLITS, f"split {t.get('split')!r}")
            c(f"task.{tid}.not_in_training", t.get("prompt_sha256") not in training, "prompt not in the declared training hashes")

        results = record.get("results")
        if not isinstance(results, list):
            raise Bad("results must be a list")
        ids = [r.get("task_id") for r in results if isinstance(r, dict)]
        if len(ids) != len(results):
            raise Bad("a result must be an object")
        unknown = sorted({i for i in ids if i not in T}, key=str)
        dup = sorted({i for i in ids if ids.count(i) > 1}, key=str)
        missing = sorted(set(T) - set(ids))
        c("results.coverage", not unknown and not dup and not missing,
          f"unknown {unknown}, duplicate {dup}, missing {missing}" if (unknown or dup or missing) else "one result per task")
        scores, failing, near = {}, [], []
        for r in results:
            tid = r.get("task_id")
            if tid not in T or tid in scores:
                continue
            gr = r.get("grade") if isinstance(r.get("grade"), dict) else {}
            s = _num(gr.get("score"), f"{tid}.score", 0.0, 1.0)
            scores[tid] = s
            passed = s >= threshold
            c(f"result.{tid}.output", _hex(r.get("output_sha256")), "output committed by SHA-256")
            c(f"result.{tid}.grader", gr.get("grader_id") == gid, f"graded by {gr.get('grader_id')!r}")
            c(f"result.{tid}.pass_flag", gr.get("passed") is passed, f"stated {gr.get('passed')!r}, score {s:g} vs threshold {threshold:g}")
            if not passed:
                failing.append(tid)
                if s >= threshold - band - 1e-12:
                    near.append(tid)
        failing, near = sorted(failing), sorted(near)
        n = len(scores)
        mean = round(sum(scores.values()) / n, 4) if n else 0.0
        npass = n - len(failing)

        pub = record.get("published") if isinstance(record.get("published"), dict) else {}
        c("published.n_tasks", pub.get("n_tasks") == len(T), f"stated {pub.get('n_tasks')!r}, tasks {len(T)}")
        c("published.passed", pub.get("passed") == npass, f"stated {pub.get('passed')!r}, recomputed {npass}")
        ps = pub.get("score")
        c("published.score", isinstance(ps, (int, float)) and not isinstance(ps, bool) and math.isfinite(ps) and abs(ps - mean) <= 1e-4,
          f"stated {ps!r}, recomputed mean {mean}")
        pf = pub.get("failures") if isinstance(pub.get("failures"), list) else None
        c("published.failures", pf is not None and sorted(map(str, pf)) == failing, f"stated {pf!r}, recomputed {failing}")
        pn = pub.get("near_misses") if isinstance(pub.get("near_misses"), list) else None
        c("published.near_misses", pn is not None and sorted(map(str, pn)) == near, f"stated {pn!r}, recomputed {near}")

        def all_ok(pred):
            return all(x["ok"] for x in checks if pred(x["id"]))

        computed = {"task_recorded": all_ok(lambda i: i.startswith("task.") and not i.endswith(".not_in_training")),
                    "model_recorded": all_ok(lambda i: i.startswith("model.")),
                    "grader_recorded": all_ok(lambda i: i in ("grader.recorded", "grader.rubric")),
                    "grader_independent": all_ok(lambda i: i == "grader.independent"),
                    "failures_published": all_ok(lambda i: i in ("published.failures", "published.near_misses")),
                    "score_recomputed": all_ok(lambda i: i.startswith("result.") or i in
                                               ("results.coverage", "published.n_tasks", "published.passed", "published.score")),
                    "no_training_overlap": all_ok(lambda i: i.endswith(".not_in_training"))}
        claims = record.get("claims") if isinstance(record.get("claims"), dict) else {}
        for k in sorted(claims):
            c(f"claims.{k}", k in computed and claims[k] == computed[k], f"claimed {claims[k]!r}, computed {computed.get(k)!r}")
        summary.update({"computed": computed, "score": mean, "passed": npass, "n_tasks": len(T),
                        "failures": failing, "near_misses": near})
    except Bad as e:
        checks.append({"id": "input.well_formed", "ok": False, "detail": str(e)})
        return "UNSOUND", checks, summary
    except Exception as e:  # noqa: BLE001
        checks.append({"id": "input.error", "ok": False, "detail": f"{type(e).__name__}: {e}"[:200]})
        return "UNSOUND", checks, summary
    return ("SOUND" if all(x["ok"] for x in checks) else "UNSOUND"), checks, summary


# ---------------------------------------------------------------------------- offline verifier (no CAIN imports)
# Every name below is prefixed _kit_verifier_ / _b64 / _hl / _json / _sys so it cannot shadow a name in the rules above.
import base64 as _b64, hashlib as _hl, json as _json, sys as _sys
_KIT_VERIFIER_GENESIS = _hl.sha256(b"cain42.a75.benchevidence.genesis").hexdigest()


def _kit_verifier_canon(o):
    return _json.dumps(o, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()


def _kit_verifier_sha(o):
    return _hl.sha256(_kit_verifier_canon(o)).hexdigest()


def _kit_verifier_main(argv):
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
    if len(argv) < 2:
        print("usage: verify_offline.py run.json --pinned-key BASE64"); return 2
    run = _json.load(open(argv[1]))
    pin = argv[argv.index("--pinned-key") + 1] if "--pinned-key" in argv else None
    problems, prev, recs = [], _KIT_VERIFIER_GENESIS, run.get("records") or []
    for i, r in enumerate(recs):
        body = {k: v for k, v in r.items() if k not in ("record_hash", "signature_b64")}
        if _kit_verifier_sha(body) != r.get("record_hash"): problems.append(f"record {i}: record_hash does not match")
        if body.get("subject_sha256") != _kit_verifier_sha(body.get("subject")): problems.append(f"record {i}: subject_sha256 does not match")
        if pin and r.get("public_key_b64") != pin: problems.append(f"record {i}: different key than the pinned one")
        try:
            Ed25519PublicKey.from_public_bytes(_b64.b64decode(pin or r.get("public_key_b64"))).verify(
                _b64.b64decode(r.get("signature_b64", "")), str(r.get("record_hash")).encode())
        except Exception:
            problems.append(f"record {i}: signature does not verify")
        if r.get("prev_record_hash") != prev: problems.append(f"record {i}: chain link broken")
        v, checks, _ = evaluate(r.get("subject"))
        if v != r.get("verdict") or checks != r.get("checks"):
            problems.append(f"record {i}: re-running the rules gives {v}, the record says {r.get('verdict')}")
        prev = r.get("record_hash")
    if run.get("run_hash") != _kit_verifier_sha([r.get("record_hash") for r in recs]): problems.append("run_hash does not match")
    for p in problems: print("  -", p)
    if not pin: print("WARNING: no --pinned-key given; the signature was checked against the key inside the file")
    print("VERDICT:", "VERIFIED" if not problems else "TAMPERED OR INVALID", f"(CAIN-A75-056, {len(recs)} record(s), verdicts {run.get('verdicts')})")
    return 0 if not problems else 1


if __name__ == "__main__":
    _sys.exit(_kit_verifier_main(_sys.argv))
