#!/usr/bin/env python3
"""Standalone verifier for the 2026-11-16 cluster restore evidence bundle.

Checks the artifacts in this directory and recomputes the facts the README claims.
Imports no CAIN code: standard library only.

    python3 verify_restore_2026_11_16.py

Exit 0 = every check passed. Exit 1 = a check failed (the message says which).
This verifies the RECORDED evidence. It does not talk to the live clusters; run the curl
commands in README section 5 for that.
"""
from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
fails: list[str] = []
checks = 0


def check(name: str, cond: bool, detail: str = "") -> None:
    global checks
    checks += 1
    if cond:
        print(f"PASS {name}")
    else:
        print(f"FAIL {name}{(' — ' + detail) if detail else ''}")
        fails.append(name)


def load(rel: str):
    return json.loads((HERE / rel).read_text())


def text(rel: str) -> str:
    return (HERE / rel).read_text(errors="replace")


# ---------------------------------------------------------------- integrity
manifest = load("MANIFEST.json")
listed = manifest["files"]
check("manifest declares a schema", manifest.get("schema") == "cain42.evidence.manifest.v1")
check("manifest lists at least 10 files", len(listed) >= 10, f"listed={len(listed)}")

for rel, meta in listed.items():
    p = HERE / rel
    if not p.exists():
        check(f"file exists: {rel}", False, "missing")
        continue
    raw = p.read_bytes()
    got = hashlib.sha256(raw).hexdigest()
    check(f"sha256 matches: {rel}", got == meta["sha256"], f"expected {meta['sha256'][:12]} got {got[:12]}")
    check(f"size matches: {rel}", len(raw) == meta["bytes"], f"{meta['bytes']} != {len(raw)}")

# ---------------------------------------------------------------- cain-mr-03 failure
state = load("pre-fix/mr3-node1-state.json")
check("mr3-node-1 was not running before the fix", state.get("Running") is False)
check("mr3-node-1 exit code was 3", state.get("ExitCode") == 3, str(state.get("ExitCode")))
node1_log = text("pre-fix/mr3-node1.log")
check("mr3-node-1 failed on EADDRINUSE for 10.88.0.1:29811",
      "address already in use" in node1_log and "29811" in node1_log)

ps = text("pre-fix/docker-ps.txt")
check("the retired cain-mr2-node-1 was running before the fix",
      "cain-mr2-node-1\tUp" in ps)
sentinel = text("pre-fix/sentinel-mr2-restarts.log")
check("the guardian logged an auto-restart of cain-mr2-node-1",
      "restarting Docker container cain-mr2-node-1" in sentinel)
soak_tail = text("pre-fix/soak-tail.log")
check("the soak was logging check errors before the fix", "CHECK ERROR" in soak_tail)

# ---------------------------------------------------------------- cain-mr-01 invalid quorum
bad = load("mr01/invalid-quorum-response.json")
check("pre-fix write reported COMMITTED", bad.get("status") == "COMMITTED")
check("pre-fix write certificate was INVALID",
      str(bad.get("quorum_certificate_status", "")).startswith("INVALID"))
check("pre-fix write had exactly 1 accepted signer",
      bad.get("quorum_certificate_accepted_signers") == 1, str(bad.get("quorum_certificate_accepted_signers")))
check("1 signer is below the quorum of 3", bad.get("quorum_size") == 3 and 1 < bad.get("quorum_size"))
check("the defect is a COMMITTED-without-quorum case",
      bad.get("status") == "COMMITTED" and bad.get("quorum_certificate_accepted_signers", 0) < bad.get("quorum_size", 0))

mr01_before = load("mr01/node-4-status.json")
check("mr01 node-4 was in SAFE_MODE before the fix", mr01_before.get("health_state") == "SAFE_MODE")
check("mr01 node-4 recorded a WAL disk I/O error",
      "disk I/O error" in str(mr01_before.get("wal_write_failure", "")))

probes = [ln for ln in text("mr01/post-fix-write-probes.txt").splitlines() if ln.strip()]
check("three post-fix write probes are recorded", len(probes) == 3, str(len(probes)))
check("every post-fix mr01 write is quorum-VALID with 4 signers",
      all("'quorum_certificate_status': 'VALID'" in ln and "'quorum_certificate_accepted_signers': 4" in ln
          for ln in probes))

# ---------------------------------------------------------------- cain-mr-03 after
for n in (1, 2, 3, 4):
    d = load(f"post-fix/mr03-node-{n}.json")
    check(f"mr03 node-{n} is HEALTHY", d.get("health_state") == "HEALTHY", str(d.get("health_state")))
    check(f"mr03 node-{n} reports the cluster cain-mr-03", d.get("cluster_id") == "cain-mr-03")
hashes = {load(f"post-fix/mr03-node-{n}.json").get("application_state_hash") for n in (1, 2, 3, 4)}
heights = {load(f"post-fix/mr03-node-{n}.json").get("commit_index") for n in (1, 2, 3, 4)}
check("all 4 mr03 replicas share one application_state_hash", len(hashes) == 1, str(hashes))
check("all 4 mr03 replicas share one commit_index", len(heights) == 1, str(heights))

# ---------------------------------------------------------------- cain-mr-01 after
for n in (1, 2, 3, 4):
    d = load(f"post-fix/mr01-node-{n}.json")
    check(f"mr01 node-{n} is HEALTHY", d.get("health_state") == "HEALTHY", str(d.get("health_state")))
h1 = {load(f"post-fix/mr01-node-{n}.json").get("application_state_hash") for n in (1, 2, 3, 4)}
check("all 4 mr01 replicas share one application_state_hash", len(h1) == 1, str(h1))

# ---------------------------------------------------------------- gateway verdicts
g3 = load("post-fix/gateway-health-mr03.json")
check("gateway reports cain-mr-03 HEALTHY", g3.get("verdict") == "HEALTHY", str(g3.get("verdict")))
check("gateway reports 4 reachable mr03 replicas", g3.get("replicas_reachable") == 4, str(g3.get("replicas_reachable")))
g1 = load("post-fix/gateway-health-mr01.json")
check("gateway reports cain-mr-01 HEALTHY", g1.get("verdict") == "HEALTHY", str(g1.get("verdict")))
check("gateway reports 4 reachable mr01 replicas", g1.get("replicas_reachable") == 4, str(g1.get("replicas_reachable")))
g2 = load("post-fix/gateway-health-mr02.json")
check("gateway reports the retired cain-mr-02 HALTED", g2.get("verdict") == "HALTED", str(g2.get("verdict")))
check("gateway reports 0 reachable mr02 replicas", g2.get("replicas_reachable") == 0, str(g2.get("replicas_reachable")))

# ---------------------------------------------------------------- containers + sentinel
c = text("post-fix/containers.txt")
check("the live replica cain-mr3-node-1 is running", "cain-mr3-node-1\tUp" in c)
check("the retired cain-mr2-node-1 is not running", "cain-mr2-node-1\t" not in c)
guard = text("post-fix/retired-guard-proof.txt")
check("the guardian refuses to start a retired container",
      guard.count("-> False") == 4 and guard.count("Refusing to start retired container") >= 4)
check("the retired container stayed stopped after the guard proof",
      "running=false" in guard)
s = load("post-fix/sentinel-state.json")
check("the guardian reports the host HEALTHY", s.get("overall_status") in ("HEALTHY", "HEALTHY_WITH_WARNINGS"),
      str(s.get("overall_status")))
check("the guardian no longer monitors the retired cluster",
      "cluster_cain-mr-02" not in s.get("components", {}))
check("the guardian monitors cain-mr-03", s.get("components", {}).get("cluster_cain-mr-03", {}).get("status") == "HEALTHY",
      str(s.get("components", {}).get("cluster_cain-mr-03", {}).get("status")))

# ---------------------------------------------------------------- soak honesty
sv = text("post-fix/soak-verify.txt")
check("the new soak checkpoint is VALID", "VALID    checkpoint-0000.json" in sv)
check("the new soak verdict says RUNNING, not passed", "VERDICT: RUNNING" in sv)
check("the new soak is 48 h planned", "of 48.0 h" in sv)
restore = load("RESTORE.json")
check("the bundle explicitly does not claim a passed 48-hour soak",
      "48-hour soak passed" in restore.get("not_claimed", []))
check("the soak verdict in RESTORE.json is RUNNING",
      str(restore.get("soak", {}).get("verdict", "")).startswith("RUNNING"))
check("no 48h/72h pass claim in the README",
      "48-hour soak has **not** passed" in text("README.md"))

# ---------------------------------------------------------------- follow-ups
vtest = text("followups/soak-verdict-tests.txt")
check("the soak verdict fix has passing tests", "8 passed" in vtest)
dck = load("followups/soak-d-checkpoint-0000.json")
check("the running soak records per-host free disk", len(dck.get("disk_free_gb", {})) == 4, str(dck.get("disk_free_gb")))
check("the running soak records the split check-error counters",
      {"check_errors_expected", "unexpected_check_errors", "max_unexpected_check_streak"} <= set(dck.get("counters", {})))
dv = text("followups/soak-d-checkpoint-0000-verify.txt")
check("the new soak checkpoint is VALID", "VALID    checkpoint-0000.json" in dv)
check("the new soak is still RUNNING, not passed", "VERDICT: RUNNING" in dv)
c_old = load("followups/soak-c-superseded.json")
check("the superseded run is labelled as short, not as a pass",
      float(c_old.get("elapsed_hours", 99)) < 1.0, str(c_old.get("elapsed_hours")))
check("the verdict fix is recorded as fixed", any(f.get("id") == "F5" and str(f.get("status", "")).startswith("FIXED")
                                                  for f in restore.get("findings", [])))
check("the disk reclaim is recorded", "reclaimed" in str(restore.get("followups", {}).get("disk", "")))
fault = text("followups/soak-live-fault-window.txt")
check("a live injected fault was observed", "fault: killed" in fault and "fault: restarted" in fault)
check("the injected fault produced no unexpected error",
      "'unexpected_check_errors': 0" in fault and "'max_unexpected_check_streak': 0" in fault)

# ---------------------------------------------------------------- result
print()
if fails:
    print(f"{checks - len(fails)}/{checks} checks passed; FAILED: {fails}")
    sys.exit(1)
print(f"{checks}/{checks} checks passed")
print("NOT verified here: the live clusters (run the curl checks), a passed 48-hour soak, "
      "the root cause of the WAL I/O error, or any third-party assessment.")
