#!/usr/bin/env python3
"""
verify_P060.py — Verification script for Addendum 60 (SpectralPairing)

Checks:
1. E_Pl is computed correctly from CODATA Planck mass and TOE constants
2. The spectral gap E_Pl - E_R matches the body derivation
3. The abstract values (68.10, 11.60) are within tolerance of the body values

Constants (TOE):
    ALPHA_INV = 4π³ + π² + π
    mu1 = 16π³/5 + 3π²/4 + 2π/3
    MU = mu1 / ALPHA_INV
"""

import mpmath
import sys

mpmath.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}")

# TOE constants
ALPHA_INV = 4 * mpmath.pi**3 + mpmath.pi**2 + mpmath.pi
mu1 = mpmath.mpf(16) * mpmath.pi**3 / 5 + mpmath.mpf(3) * mpmath.pi**2 / 4 + mpmath.mpf(2) * mpmath.pi / 3
MU = mu1 / ALPHA_INV

# Physical inputs
m_e = mpmath.mpf("0.51100")           # MeV (electron mass)
M_Pl_GeV = mpmath.mpf("1.22090e19")  # GeV (CODATA Planck mass)
M_Pl_MeV = M_Pl_GeV * 1000            # MeV

# Canonical E_R from Addendum 53 (anchored to Δm²₃₁)
E_R = mpmath.mpf("56.502")

# --- Compute E_Pl ---
ratio = M_Pl_MeV / m_e
ln_ratio = mpmath.log(ratio)
E_Pl = mpmath.pi + ln_ratio / MU
gap = E_Pl - E_R

print(f"TOE constants:")
print(f"  ALPHA_INV = {float(ALPHA_INV):.6f}")
print(f"  MU = mu1/ALPHA_INV = {float(MU):.6f}")
print(f"")
print(f"Planck computation:")
print(f"  M_Pl = {float(M_Pl_GeV):.5e} GeV = {float(M_Pl_MeV):.5e} MeV")
print(f"  m_e  = {float(m_e)} MeV")
print(f"  M_Pl/m_e = {float(ratio):.6e}")
print(f"  ln(M_Pl/m_e) = {float(ln_ratio):.6f}")
print(f"  E_Pl = pi + ln/MU = {float(E_Pl):.6f}")
print(f"  E_R  = {float(E_R):.3f}")
print(f"  gap  = E_Pl - E_R = {float(gap):.6f}")
print()

# --- Check 1: E_Pl within [67.8, 68.4] ---
check(1, f"E_Pl = {float(E_Pl):.4f} in [67.8, 68.4]",
      67.8 < float(E_Pl) < 68.4)

# --- Check 2: E_Pl matches body-derived value ≈ 68.10 within 0.05 ---
E_Pl_body = mpmath.mpf("68.10")
diff_EPl = abs(E_Pl - E_Pl_body)
check(2, f"|E_Pl - 68.10| = {float(diff_EPl):.4f} < 0.05", diff_EPl < 0.05)

# --- Check 3: gap = E_Pl - E_R is approximately 11.59 ---
gap_body = mpmath.mpf("11.59")
diff_gap = abs(gap - gap_body)
check(3, f"|gap - 11.59| = {float(diff_gap):.4f} < 0.05", diff_gap < 0.05)

# --- Check 4: gap reasonableness check (not trivially 0) ---
check(4, f"gap = {float(gap):.4f} > 10 (non-trivial)", float(gap) > 10.0)

# --- Check 5: abstract values (68.10, 11.60) consistent with body (≤ 0.05 error) ---
abstract_EPl = mpmath.mpf("68.10")
abstract_gap  = mpmath.mpf("11.60")
check(5, f"abstract E_Pl=68.10 matches computed {float(E_Pl):.4f} within 0.05",
      abs(abstract_EPl - E_Pl) < 0.05)
check(6, f"abstract gap=11.60 matches computed {float(gap):.4f} within 0.05",
      abs(abstract_gap - gap) < 0.05)

# --- Check 6: stale abstract values (68.55, 12.05) are NOT close to body ---
stale_EPl = mpmath.mpf("68.55")
stale_gap  = mpmath.mpf("12.05")
check(7, f"stale value 68.55 is {float(abs(stale_EPl-E_Pl)):.4f} away from "
         f"computed {float(E_Pl):.4f} (correctly rejected)",
      abs(stale_EPl - E_Pl) > 0.3)
check(8, f"stale gap 12.05 is {float(abs(stale_gap-gap)):.4f} away from "
         f"computed {float(gap):.4f} (correctly rejected)",
      abs(stale_gap - gap) > 0.3)

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