#!/usr/bin/env python3
"""
verify_P103.py -- Addendum 103: bottom-quark scheme correction.

This verifier checks the bottom mass spectral formula, the F4->G2 correction
factor, and the sensitivity/NLO arithmetic. The headline corrected bottom mass
is reproducible if one uses the older continuous-moment MU, but the displayed
MU formula is the same stale formula flagged in P100 and the alpha_s^2 estimate
has a factor-10 arithmetic error.
"""

import math
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from verify_common import CheckResult, Verifier


class ModernVerifier(Verifier):
    """Local output adapter: inherits Verifier's tolerance logic unchanged,
    emits the corpus's modern check-line format (numbered [PASS]/[FAIL]
    lines, computed/claimed as indented info lines)."""

    def __init__(self, name: str):
        self.name = name
        self.results = []
        print(name)

    def record(self, label, ok, computed="", claimed="", detail=""):
        self.results.append(CheckResult(label, ok, computed, claimed, detail))
        n = len(self.results)
        desc = label if (ok or not detail) else f"{label} -- {detail}"
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"        computed: {computed}")
            print(f"        claimed : {claimed}")
        if ok and detail:
            print(f"        {detail}")
        return ok

    def summary(self):
        passed = sum(r.ok for r in self.results)
        failed = len(self.results) - passed
        print(f"\n{'='*60}\nRESULT: {passed} PASS / {failed} FAIL")
        return 1 if failed else 0


v = ModernVerifier("P103 -- Bottom-Quark Scheme Correction")

ROOT = Path(__file__).resolve().parents[1]
TEX = (ROOT / "103_Addendum_BottomQuarkScheme.tex").read_text()

PI = math.pi
M_E = 0.511
M_B_PDG = 4180.0
M_B_SIGMA = 30.0
ALPHA_MB = 0.2269

MU0 = 4 * PI**3 + PI**2 + PI
MU1 = 16 * PI**3 / 5 + 3 * PI**2 / 4 + 2 * PI / 3
MU = MU1 / MU0
MU_DISPLAY = (PI**2 + PI**4 + 16 * PI**6) / (PI + PI**2 + 4 * PI**3)
E_B = PI ** (7 / 3)


def pct(value: float, target: float) -> float:
    return 100 * (value - target) / target


v.check(
    "displayed MU formula",
    MU_DISPLAY,
    0.79334,
    rel=1e-3,
    detail="Expected fail: the displayed P100-style formula evaluates to about 113, not 0.79334.",
)
v.check("continuous-moment MU used by numerics", MU, 0.79334, rel=1e-5)
v.check(
    "pi^(7/3) printed approximation",
    E_B,
    14.452,
    rel=1e-4,
    detail="Expected fail: pi^(7/3) is about 14.45494; 14.452 is too low for the printed precision.",
)
v.check("pi^(7/3) formula value used by mass", E_B, 14.4549, rel=5e-6)
exponent = MU * (E_B - PI)
v.check(
    "bottom exponent printed in constants section",
    exponent,
    8.9837,
    rel=5e-4,
    detail="Expected fail: exact exponent is about 8.9753; exp(8.9837) would not give the listed mass.",
)
v.check("exp(exact bottom exponent)", math.exp(exponent), 7906, rel=5e-4)
m_b_toe = M_E * math.exp(exponent)
v.check("raw TOE bottom mass", m_b_toe, 4039.8, rel=5e-5)
v.check("raw residual percent", pct(4039.8, M_B_PDG), -3.35, rel=5e-3)

delta_geom = (4 / 9) * ALPHA_MB / PI
m_b_corr = 4039.8 * (1 + delta_geom)
v.check("alpha_s(m_b)/pi", ALPHA_MB / PI, 0.07222, rel=1e-4)
v.check("geometric correction delta", delta_geom, 0.03210, rel=5e-4)
v.check("correction factor", 1 + delta_geom, 1.03210, rel=5e-5)
v.check("abstract correction factor 1.03212", 1 + delta_geom, 1.03212, rel=5e-5)
v.check("corrected bottom mass from listed 4039.8", m_b_corr, 4169.6, abs_tol=0.2)
v.check("corrected residual percent", pct(m_b_corr, M_B_PDG), -0.25, rel=2e-2)
v.record("corrected value is inside one sigma", abs(m_b_corr - M_B_PDG) <= M_B_SIGMA, f"{m_b_corr:.3f}", "4150..4210")

for alpha, claimed_mass in [(0.2299, 4171.3), (0.2239, 4167.5)]:
    delta = (4 / 9) * alpha / PI
    v.check(f"sensitivity mass alpha_s={alpha}", 4039.8 * (1 + delta), claimed_mass, abs_tol=0.4)

c2_qcd = 1099 / 216
delta_nlo = (4 / 9) * (ALPHA_MB**2 / PI**2) * c2_qcd
v.check("QCD c2 = 1099/216", c2_qcd, 5.09, rel=5e-4)
v.check(
    "alpha_s^2 geometric correction from displayed formula",
    delta_nlo,
    0.00118,
    rel=5e-2,
    detail="Expected fail: the displayed formula gives about 0.0118, not 0.00118.",
)
m_b_nlo = 4039.8 * (1 + delta_geom + delta_nlo)
v.check(
    "NLO corrected bottom mass",
    m_b_nlo,
    4174.4,
    abs_tol=0.5,
    detail="Expected fail: using the displayed c2=5.09 gives about 4217 MeV; 4174.4 requires delta2 about 0.00118.",
)
v.check(
    "NLO residual percent",
    pct(m_b_nlo, M_B_PDG),
    -0.13,
    rel=1e-1,
    detail="Expected fail: the displayed alpha_s^2 formula overshoots PDG by about +0.89%.",
)

delta_charm = (1 / 3) * 0.387 / PI
v.check("charm trial correction", delta_charm, 0.0411, rel=1e-3)
v.check("charm corrected mass estimate", 1274 * (1 + delta_charm), 1327, rel=1e-3)
delta_top = (3 / 4) * 0.108 / PI
v.check("top trial correction", delta_top, 0.02579, rel=5e-4)
v.check("top corrected mass estimate", 173.5 * (1 + delta_top), 178.0, rel=5e-4)

v.record(
    "all Peirce blocks are not treated differently under entrywise G2 action without proof",
    "In contrast, $\\mathcal{P}_{12}$ and $\\mathcal{P}_{13}$ are not pure $G_2$-modules" not in TEX,
    "paper says P23 is a pure G2 module while P12/P13 are not",
    "entrywise G2 action makes each octonionic Peirce block R+ImO",
    "Expected fail: the claimed unique P23 module status needs an additional embedding argument; entrywise Aut(O) acts on every Peirce block as 1+7.",
)
v.record(
    "scheme conversion is derived from standard MSbar matching rather than postulated",
    "Geometric scheme correction" not in TEX,
    "paper defines a Coxeter-ratio scheme correction",
    "standard scheme-matching derivation",
    "Expected fail: the 4/9 factor is structurally motivated but not derived from ordinary QCD scheme matching.",
)

sys.exit(v.summary())
