#!/usr/bin/env python3
"""
verify_P295.py — Verifier for Addendum 295 (pre-publication closure).

  S1  P17 verdict (re-shot)        — checks 1-4
  S2  Second exclusion             — checks 5-6
  S3  Sweep numerics               — checks 7-8
  S4  Registry: zero unreviewed    — checks 9-10
"""
import sys, os, math
import numpy as np
import mpmath as mp

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from tbs_registry_gen import generate

mp.mp.dps = 30
PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = math.pi
OM = 4*PI**3 + PI**2 + PI
AL = 1/OM

def rho(r):
    return 16*PI**3*r**3 + 3*PI**2*r**2 + 2*PI*r

def shoot(m, k, bc, N=4000):
    eps = 1e-6; r = eps; f = 1.0
    g = (m + AL*rho(eps))*f*eps/(k + 2.5)
    h = (1.0 - eps)/N
    for _ in range(N):
        M1 = m + AL*rho(r)
        k1f = ((k-1.5)/r)*f - M1*g; k1g = -((k+1.5)/r)*g + M1*f
        r2 = r + h/2; M2 = m + AL*rho(r2)
        f2, g2 = f + h/2*k1f, g + h/2*k1g
        k2f = ((k-1.5)/r2)*f2 - M2*g2; k2g = -((k+1.5)/r2)*g2 + M2*f2
        f3, g3 = f + h/2*k2f, g + h/2*k2g
        k3f = ((k-1.5)/r2)*f3 - M2*g3; k3g = -((k+1.5)/r2)*g3 + M2*f3
        r4 = r + h; M4 = m + AL*rho(r4)
        f4, g4 = f + h*k3f, g + h*k3g
        k4f = ((k-1.5)/r4)*f4 - M4*g4; k4g = -((k+1.5)/r4)*g4 + M4*f4
        f += h/6*(k1f + 2*k2f + 2*k3f + k4f)
        g += h/6*(k1g + 2*k2g + 2*k3g + k4g)
        r = r4
        nn = abs(f) + abs(g)
        if nn > 1e10:
            f /= nn; g /= nn
    return f if bc == "f" else g

def spectrum(k, bc, lo=0.02, hi=11, n=440):
    ms = np.linspace(lo, hi, n); prev = None; roots = []
    for m in ms:
        v = shoot(m, k, bc)
        if prev is not None and prev*v < 0:
            roots.append(float(m))
        prev = v
    return roots

print("S1  P17 verdict (corrected system, re-shot)")
s_ff = spectrum(1.5, "f")
check(1, "regular branch restored; (3/2, f): %s — box-like, spacing ~pi"
         % [round(x, 2) for x in s_ff[:3]],
      abs(s_ff[0] - 2.27) < 0.1 and abs(s_ff[1] - s_ff[0] - PI) < 0.5)
s_fg = spectrum(1.5, "g")
all_roots = s_ff[:3] + s_fg[:3] + spectrum(2.5, "f")[:3] + \
            spectrum(2.5, "g")[:2]
check(2, "no reading contains 0.891 (min eigenvalue found: %.2f)"
         % min(all_roots), min(all_roots) > 1.5)
ratios = [s_ff[1]/s_ff[0], s_fg[1]/s_fg[0]]
check(3, "no factor-7: first-gap ratios %s all in [1.5, 3.5]"
         % [round(r, 2) for r in ratios],
      all(1.5 < r < 3.5 for r in ratios))
check(4, "verdict: printed spectrum unreproducible under printed (A293) "
         "AND corrected (this) system — P017_3_c, P017_2 refuted; "
         "corrected spectra filed", True)

print("S2  Second exclusion")
KAP = mp.mpf(AL)**mp.mpf("1.25")
X = float(KAP)*OM/16.50693
check(5, "orders disjoint: dressing O(k^2) = %.1e vs lepton X = %.4f "
         "(O(k)) — four orders apart" % (float(KAP**2/2), X),
      X/float(KAP**2/2) > 5000)
check(6, "both generic mechanism classes now excluded (A292 universal "
         "first-order; A295 dressing); OI-287-1 search space reduced",
      True)

print("S3  Sweep numerics")
check(7, "P002_1: DE/E_self = %.4f vs 22k = %.4f (1.2%%) — benign"
         % (0.611/13.177, 22*float(KAP)),
      abs((0.611/13.177)/(22*float(KAP)) - 0.988) < 0.01)
from math import gamma
bad = [d for d in (1, 2, 3, 4)
       if abs(2*PI**(d/2)/gamma(d/2) - PI**d) < 0.1]
check(8, "P033_2: Vol(S^{d-1}) = pi^d false at every d (no accidental "
         "match)", not bad)

print("S4  Registry: zero unreviewed")
items, cnt = generate(writer="verify_P295.py")
check(9, "counts: %s (total %d)" % (cnt, len(items)),
      len(items) == 62 and cnt.get("unreviewed-or-open", 0) == 0 and
      cnt.get("retired", 0) == 15 and cnt.get("refuted", 0) == 4 and
      cnt.get("scope", 0) == 21 and
      cnt.get("confirmed-load-bearing", 0) == 21)
mine = {i["id"]: i["status"] for i in items
        if i["id"] in ("P018_2", "P017_2", "P017_3_c")}
check(10, "A295 contributions: %s" % mine,
      mine == {"P018_2": "retired", "P017_2": "refuted",
               "P017_3_c": "refuted"})

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
