#!/usr/bin/env python3
"""
verify_P288.py — Verifier for Addendum 288 (the variance weight).

  S1  The chain                    — checks 1-4
  S2  Confrontation                — checks 5-7
  S3  Ranking and registry         — checks 8-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
KAP = AL**mp.mpf("1.25")
C18 = mp.mpf("137.035999084"); C22 = mp.mpf("137.035999177")
SIG = mp.mpf("2.1e-8")

print("S1  The chain")
f_edge = PI/OM
check(1, "layer measures: bulk+boundary+edge = (4pi^3+pi^2+pi)/Omega = 1; "
         "edge share pi/Omega = %s" % mp.nstr(f_edge, 6),
      abs((4*PI**3 + PI**2 + PI)/OM - 1) < mp.mpf(10)**-35)
def predict(edge_share):
    return OM*mp.sqrt(1 - KAP**2*(1 - edge_share))
check(2, "Link 2 (equipartition) + Link 3 (edge unobserved): observed "
         "variance = kappa^2 (1 - pi/Omega); A = %s kappa"
         % mp.nstr(mp.sqrt(1 - f_edge), 6),
      abs(mp.sqrt(1 - f_edge) - mp.mpf("0.98847")) < 1e-5)
a_pred = predict(f_edge)
check(3, "Link 4 (linear average): alpha^-1_pred = %s"
         % mp.nstr(a_pred, 12),
      abs(a_pred - mp.mpf("137.035999236")) < 1e-8)
check(4, "parametrized machinery: edge share s -> prediction; s = 0 gives "
         "%s (A283's bare kappa), s = pi/Omega marked as measure-share "
         "value" % mp.nstr(predict(0), 12),
      predict(0) < a_pred < OM)

print("S2  Confrontation")
check(5, "deficit %s vs CODATA-22 %s (agreement %s)"
         % (mp.nstr(1 - a_pred/OM, 6), mp.nstr(1 - C22/OM, 6),
            mp.nstr(abs((1-a_pred/OM)/(1-C22/OM) - 1), 3)),
      abs((1 - a_pred/OM)/(1 - C22/OM) - 1) < 3e-4)
check(6, "A inside A283's measured 0.9887(2) at 2e-4: |0.98847 - 0.98857| "
         "= %s" % mp.nstr(abs(mp.sqrt(1-f_edge) - mp.mpf("0.988566")), 3),
      abs(mp.sqrt(1 - f_edge) - mp.mpf("0.988566")) < 2e-4)
t = (a_pred - C22)/SIG
check(7, "tension owned: %s sigma_exp from CODATA-22 (not absorbed)"
         % mp.nstr(t, 3), 2.0 < t < 3.5)

print("S3  Ranking and registry")
t44 = abs(OM*mp.sqrt(1 - KAP**2*(mp.mpf(1)/44 + (1 - mp.mpf(1)/44) - mp.mpf(1)/44)) - C22)/SIG  # placeholder shape
a44 = OM*mp.sqrt(1 - KAP**2*(1 - mp.mpf(1)/44))
check(8, "rival 1-1/44: fits CODATA-22 at %s sigma but possesses no "
         "chain; demoted to coincidence-watch per A245/A180 ranking"
         % mp.nstr(abs(a44 - C22)/SIG, 3), abs(a44 - C22)/SIG < 1.0)
check(9, "OI-283-1 reduced to Link 2: variance decomposition of the P03 "
         "fundamental mode by layer (computation named, not performed)",
      True)
items, cnt = generate(writer="verify_P288.py")
note = next(i["note"] for i in items if i["id"] == "P000_1_c")
check(10, "registry: P000_1_c note current (A283 measurement + A284 "
          "bracket recorded): %s..." % note[:40],
      "A283" in note and "0.9887" in note)

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