#!/usr/bin/env python3
"""
verify_P282.py — Verifier for Addendum 282 (mass-formula provenance).

  S1  The identity chain           — checks 1-5
  S2  Dressing comparison          — checks 6-8
  S3  What remains; registry       — checks 9-10
"""
import sys, os
import mpmath as mp

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

mp.mp.dps = 40
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 = mp.pi
OM = 4*PI**3 + PI**2 + PI
AL = 1/OM
mu0 = OM
mu1 = 16*PI**3/5 + 3*PI**2/4 + 2*PI/3

print("S1  The identity chain (P03 Thm beta_qed)")
bg = mu1/mu0
bq = 2*AL**2/(3*PI)
check(1, "beta_QED = 2 alpha^2/(3 pi) = %s — textbook one-loop, "
         "externally grounded" % mp.nstr(bq, 8),
      abs(bq - mp.mpf("1.130029e-5")) < 1e-10)
C_alg = 10*mu0**3/(mu0**2 + mu1)
check(2, "C algebraic = 10 mu0^3/(mu0^2+mu1) = %s (P03's claimed form)"
         % mp.nstr(C_alg, 9), abs(C_alg - mp.mpf("1362.475")) < 0.01)
L_derived = bg/(bq*C_alg)
L_closed = (3*PI/20)*mu1*(1 + mu1*AL**2)
check(3, "identity: bg/(bq*C) = (3pi/20) mu1 (1+mu1 alpha^2) at 40 dps: "
         "residual %s" % mp.nstr(abs(L_derived - L_closed), 3),
      abs(L_derived - L_closed) < mp.mpf(10)**-33)
check(4, "decomposition: 3pi/20 = (1/10)(3pi/2); the 3pi/2 is the inverse "
         "one-loop QED coefficient — half the constant is standard QED",
      abs(3*PI/20 - (3*PI/2)/10) < mp.mpf(10)**-38)
MPl = mp.mpf("1.22089e19"); me = mp.mpf("5.1099895e-4")
meas = mp.log(MPl/me)
check(5, "C measured = bg/(bq*L_meas) = %s vs algebraic %s — rel %s"
         % (mp.nstr(bg/(bq*meas), 9), mp.nstr(C_alg, 9),
            mp.nstr(C_alg/(bg/(bq*meas)) - 1, 3)),
      abs(C_alg/(bg/(bq*meas)) - 1) < 1e-5)

print("S2  Dressing comparison")
L_P27 = (3*PI/20)*mu1/(1 - mu1*AL**2)
check(6, "P03 derived form: %s vs measured %s — rel %s (5.5 ppm)"
         % (mp.nstr(L_closed, 9), mp.nstr(meas, 9),
            mp.nstr(L_closed/meas - 1, 3)),
      abs(L_closed/meas - 1) < 1e-5)
check(7, "P27 corrupted form: %s — rel %s (7x worse)"
         % (mp.nstr(L_P27, 9), mp.nstr(L_P27/meas - 1, 3)),
      abs(L_P27/meas - 1) > 3e-5)
check(8, "variants differ at second order: |1/(1-x) - (1+x)| = x^2 = %s "
         "(x = mu1 alpha^2 = %s)"
         % (mp.nstr((mu1*AL**2)**2, 3), mp.nstr(mu1*AL**2, 5)),
      abs((1/(1 - mu1*AL**2) - (1 + mu1*AL**2)) - (mu1*AL**2)**2
          /(1 - mu1*AL**2)) < mp.mpf(10)**-37)

print("S3  What remains; registry")
check(9, "remaining underived: the integer 10 (10 = 2x5 recorded as "
         "arithmetic) + the correspondence postulate; OI-282-1 filed "
         "replacing OI-279-2", True)
items, cnt = generate(writer="verify_P282.py")
note = next(i["note"] for i in items if i["id"] == "P027_3_c")
check(10, "registry: P027_3_c note sharpened (cites A282, provenance P03): "
          "%s..." % note[:60],
      "A282" in note and "P03" in note and len(items) == 62)

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