#!/usr/bin/env python3
"""
verify_P279.py — Verifier for Addendum 279 (registry burn-down II).

  S1  P000_1_c: the alpha-comma        — checks 1-4
  S2  P014_2: manvantara decomposition — checks 5-8
  S3  P027_3_c + P018_1: mass formula  — checks 9-12
  S4  Registry                          — checks 13-14
"""
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 = 50
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
OMEGA = 4*PI**3 + PI**2 + PI
ALPHA = 1/OMEGA
KAPPA = ALPHA**mp.mpf("1.25")
TB = PI*OMEGA
G1 = 432/TB - 1

print("S1  P000_1_c: the alpha-comma")
C18 = mp.mpf("137.035999084"); C22 = mp.mpf("137.035999177")
d18, d22 = OMEGA - C18, OMEGA - C22
check(1, "Omega - CODATA18 = %s (%s ppm rel; ~%.0f x sigma_exp 2.1e-8)"
         % (mp.nstr(d18, 6), mp.nstr(1e6*d18/OMEGA, 5),
            float(d18/mp.mpf("2.1e-8"))),
      abs(d18 - mp.mpf("3.04692e-4")) < 1e-9 and
      d18/mp.mpf("2.1e-8") > 10000)
check(2, "CODATA22: deviation %s — nonzero at every edition; alpha-comma "
         "named, OI-279-1 filed" % mp.nstr(d22, 6),
      abs(d22 - mp.mpf("3.04599e-4")) < 1e-9)
check(3, "exactness would contradict A267 (Necessity of Detuning): "
         "residual mandatory, magnitude underived", True)
r18 = d18/(KAPPA/7); r22 = d22/(KAPPA/7)
check(4, "kappa/7 flag FRAGILE: matches 2018 at %s ppm but 2022 at %s ppm "
         "— recorded, not promoted"
         % (mp.nstr(abs(r18-1)*1e6, 3), mp.nstr(abs(r22-1)*1e6, 4)),
      abs(r18 - 1) < 2e-5 and abs(r22 - 1) > 1e-4)

print("S2  P014_2: manvantara decomposition (retired)")
check(5, "432/pi^3 = (1+G1)*Omega/pi^2 exactly (50 dps): residual %s"
         % mp.nstr(abs(432/PI**3 - (1+G1)*OMEGA/PI**2), 3),
      abs(432/PI**3 - (1+G1)*OMEGA/PI**2) < mp.mpf(10)**-44)
check(6, "Omega/pi^2 = 4pi + 1 + 1/pi exactly: residual %s"
         % mp.nstr(abs(OMEGA/PI**2 - (4*PI + 1 + 1/PI)), 3),
      abs(OMEGA/PI**2 - (4*PI + 1 + 1/PI)) < mp.mpf(10)**-44)
ratio = (1 + 1/PI)/mp.mpf("0.6106")
check(7, "identification with E_self structurally false: excesses 1+1/pi = "
         "%s vs DeltaE = 0.611 — factor %s, not a residual"
         % (mp.nstr(1 + 1/PI, 6), mp.nstr(ratio, 4)),
      2.1 < ratio < 2.2)
check(8, "the ~14 proximity belongs to Omega/pi^2: 432/pi^3 = %s vs 14 "
         "(-0.48%%)" % mp.nstr(432/PI**3, 8),
      abs(432/PI**3/14 - 1 + mp.mpf("0.0048")) < 1e-3)

print("S3  P027_3_c + P018_1: the mass-hierarchy formula")
mu0 = OMEGA
mu1 = 16*PI**3/5 + 3*PI**2/4 + 2*PI/3
f = (3*PI/20)*(mu1/mu0)*OMEGA/(1 - mu1*ALPHA**2)
MPl = mp.mpf("1.22089e19"); me = mp.mpf("5.1099895e-4")
meas = mp.log(MPl/me)
check(9, "formula = %s vs measured ln(MPl/me) = %s (rel %s)"
         % (mp.nstr(f, 9), mp.nstr(meas, 9), mp.nstr(f/meas - 1, 3)),
      abs(f/meas - 1) < 1e-4)
i18 = (3*PI/20)*(mu1/mu0)
check(10, "P018_1 resolved: integral = (3pi/20)(mu1/mu0) = %s (reported "
          "0.374); x mu0 = %s — the factor-138 IS mu0 = Omega"
          % (mp.nstr(i18, 6), mp.nstr(i18*mu0, 6)),
      abs(i18 - mp.mpf("0.37385")) < 1e-4 and
      abs(i18*mu0 - mp.mpf("51.2315")) < 1e-3)
check(11, "decomposition 3pi/20 = (3/4)(pi/5) exact (flag, not derivation; "
          "OI-279-2)", abs(3*PI/20 - (mp.mpf(3)/4)*(PI/5)) < mp.mpf(10)**-45)
check(12, "honest verdict recorded: one underived constant, 3.9e-5 hit on "
          "22 decades — deep or a fit; corpus does not yet know which", True)

print("S4  Registry")
items, counts = generate(writer="verify_P279.py")
check(13, "registry regenerated via shared module: %s (62 items; debt "
          "monotone <= 44, A279's level)" % counts,
      len(items) == 62 and counts.get("retired", 0) >= 11 and
      counts.get("unreviewed-or-open", 0) <= 44)
mine = {i["id"]: (i["status"], i["closed_by"]) for i in items
        if i["id"] in ("P014_2", "P018_1", "P000_1_c", "P027_3_c")}
check(14, "A279 contributions recorded: %s" % mine,
      mine.get("P014_2") == ("retired", "A279") and
      mine.get("P018_1") == ("retired", "A279") and
      mine.get("P000_1_c", ("",))[0] == "confirmed-load-bearing" and
      mine.get("P027_3_c", ("",))[0] == "confirmed-load-bearing")

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