#!/usr/bin/env python3
"""
verify_P284.py — Verifier for Addendum 284 (the edge-weighted comma).

  S1  The scan and the bracket     — checks 1-4
  S2  The conditional prediction   — checks 5-7
  S3  The discriminator; registry  — checks 8-9
"""
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
KAP = AL**mp.mpf("1.25")
C18 = mp.mpf("137.035999084"); C22 = mp.mpf("137.035999177")
SIG = mp.mpf("2.1e-8")
pred = 1 - mp.sqrt(1 - KAP**2)
c18 = ((OM - C18)/OM)/pred
c22 = ((OM - C22)/OM)/pred

print("S1  The scan and the bracket")
check(1, "required c^2: %s (18), %s (22); edition spread %s"
         % (mp.nstr(c18, 7), mp.nstr(c22, 7), mp.nstr(abs(c18-c22), 3)),
      abs(c18 - mp.mpf("0.977561")) < 1e-5 and
      abs(c22 - mp.mpf("0.977263")) < 1e-5)
edge = 1 - PI/OM
i44 = 1 - mp.mpf(1)/44
check(2, "two survivors inside the spread: edge 1-pi/Omega = %s "
         "(d22 = %s), 1-1/44 = %s (d22 = %s)"
         % (mp.nstr(edge, 7), mp.nstr(edge-c22, 3),
            mp.nstr(i44, 7), mp.nstr(i44-c22, 3)),
      abs(edge - c22) < 3e-4 and abs(i44 - c22) < 3e-4)
others = [1-KAP, 1-2*KAP, 1-AL, (1-KAP)**10, 1-mp.mpf("0.611")/OM,
          mp.mpf(31)/32]
check(3, "all other candidates miss by >= 1e-3 (min miss %s)"
         % mp.nstr(min(abs(v-c22) for v in others), 3),
      min(abs(v - c22) for v in others) > 1e-3)
check(4, "bracket recorded: c^2 = 0.9773 +/- 0.0002; identification "
         "below corpus bar (unmotivated integer beats motivated structure "
         "inside the error bar)", True)

print("S2  The conditional prediction (edge exclusion)")
a_pred = OM*mp.sqrt(1 - KAP**2*edge)
check(5, "alpha^-1_pred = Omega*sqrt(1-kappa^2(1-pi/Omega)) = %s "
         "(zero adjustable parameters)" % mp.nstr(a_pred, 12),
      abs(a_pred - mp.mpf("137.035999236")) < 1e-8)
diff = a_pred - C22
check(6, "vs CODATA-22: %s = %s sigma_exp = %s ppb — conditional, dated, "
         "falsifiable" % (mp.nstr(diff, 3), mp.nstr(diff/SIG, 3),
                          mp.nstr(1e9*diff/OM, 3)),
      2.0 < diff/SIG < 3.5)
check(7, "direction test on record: future editions toward 137.035999236 "
         "strengthen edge reading; away kill it", True)

print("S3  The discriminator; registry")
sep = OM*pred*abs(edge - i44)
check(8, "candidate separation in alpha^-1: %s = %s sigma_exp — one order "
         "more precision (0.1 ppb) decides"
         % (mp.nstr(sep, 3), mp.nstr(sep/SIG, 3)), 2.0 < sep/SIG < 4.0)
items, cnt = generate(writer="verify_P284.py")
note = next(i["note"] for i in items if i["id"] == "P000_1_c")
check(9, "registry: P000_1_c note carries the A284 bracket: %s..."
         % note[:50], "A284" in note and "0.9887" in note)

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