#!/usr/bin/env python3
"""
verify_P020.py -- Paper 20: Standard physics embedding dictionary.

This verifier checks explicit numerical claims in
toe/20_Paper_StandardPhysicsEmbedding.tex and audits which dictionary entries
are actually derived.  The rho moments, alpha value, kappa, beta, Planck-log
formula, and some dimension-ratio estimates reproduce.  The flagged issues are
mostly status/scale problems: the Higgs VEV and Higgs mass formulas do not give
246/125 GeV, the neutrino suppression estimate is far too small as written,
and several "derived/exact" mappings are physical identifications or conjectures
already flagged in P6/P37 rather than closed derivations.
"""

from __future__ import annotations

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):
    """Output adapter: modern check-line format. Tolerance logic is
    inherited unchanged from verify_common.Verifier; only printing and
    the footer differ."""

    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)
        expected = (not ok) and ("Expected" in str(detail))
        desc = f"{label} -- {detail}" if expected else label
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"       computed: {computed}")
            print(f"       claimed : {claimed}")
        if detail and not expected:
            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("P020 -- Standard Physics Embedding")

ROOT = Path(__file__).resolve().parents[2]
TEX = (ROOT / "toe" / "20_Paper_StandardPhysicsEmbedding.tex").read_text()

PI = math.pi
MU0 = 4 * PI**3 + PI**2 + PI
MU1 = 16 * PI**3 / 5 + 3 * PI**2 / 4 + 2 * PI / 3
MU2 = 16 * PI**3 / 6 + 3 * PI**2 / 5 + 2 * PI / 4
ALPHA = 1.0 / MU0
MPL_GEV = 1.22089e19
ME_GEV = 0.00051099895
V_EW = 246.0


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


v.check("mu0", MU0, 137.036, rel=3e-6)
v.check("mu1", MU1, 108.717, rel=3e-6)
v.check("mu2", MU2, 90.176, rel=5e-7)
v.check("mu1/mu0", MU1 / MU0, 0.7933, rel=6e-5)
v.check("kappa=alpha^(5/4)", ALPHA ** 1.25, 0.00213, rel=2e-3)
v.check("beta=3pi/20", 3.0 * PI / 20.0, 0.4712, rel=9e-5)

planck_log = (3.0 * PI / 20.0) * MU1 / (1.0 - MU1 * ALPHA**2)
observed_log = math.log(MPL_GEV / ME_GEV)
v.check("Planck log formula", planck_log, 51.5299, rel=1e-6)
v.check("Planck log observed comparison", observed_log, 51.5271, rel=2e-5)
v.check("Planck log percent difference", abs(pct(planck_log, observed_log)), 0.005, rel=3e-1)

v.check("strong low-energy inverse estimate", MU0 / (8.0 / 4.0), 68.5, rel=3e-4)
v.check("tree Weinberg angle", 1.0 / (1.0 + 3.0), 0.25, rel=1e-12)
v.check("SU3-corrected Weinberg angle", 3.0 / (3.0 + 8.0), 0.273, rel=1e-3)
v.check("inter-family ratio exp(mu1/mu0)", math.exp(MU1 / MU0), 2.2, rel=6e-3)

vev_formula = MPL_GEV / math.sqrt(MU0 * MU1)
v.check(
    "Higgs VEV formula M_Pl/sqrt(mu0*mu1)",
    vev_formula,
    246.0,
    rel=1e-2,
    detail="Expected fail: the formula gives about 1.0e17 GeV, matching the TeX unresolved note.",
)
higgs_formula = V_EW * math.sqrt(13.177 / MU0)
v.check(
    "Higgs mass v*sqrt(E_self/mu0)",
    higgs_formula,
    125.0,
    rel=5e-2,
    detail="Expected fail: direct substitution gives about 76.3 GeV.",
)
nu_scale_ev = ME_GEV * (V_EW / MPL_GEV) * 1e9
v.check(
    "neutrino scale m_e*(v/M_Pl)",
    nu_scale_ev,
    0.01,
    rel=0.9,
    detail="Expected fail: the displayed suppression gives about 1e-11 eV before the unspecified Z3 factor.",
)

v.record(
    "Higgs VEV issue is explicitly marked unresolved",
    "UNRESOLVED" in TEX and "naive evaluation" in TEX,
    computed="TeX comment flags missing projection/dimensionless ratio",
    claimed="not closed",
)
v.record(
    "gauge group is derived exactly without the P37 caveats",
    False,
    computed="P37 verifier flags Aut(C), Aut(H), physical SU(2)_L/U(1)_Y/color identification caveats",
    claimed="derived gauge group from division algebras exactly",
    detail="Expected status fail.",
)
v.record(
    "three families are topologically enforced without extra assumptions",
    False,
    computed="P6/P37 verifiers flag that Z3 representation labels do not forbid multiplicities by themselves",
    claimed="exactly three families / no fourth family topologically forbidden",
    detail="Expected proof-status fail.",
)
v.record(
    "strong CP solution is derived in this TeX",
    False,
    computed="paper states theta_QCD=0 but does not derive the QCD theta term or anomaly/phase argument",
    claimed="strong CP problem solved without axions",
    detail="Expected proof-status fail.",
)
v.record(
    "CKM/PMNS and full mass spectrum are supplied by explicit formulas here",
    False,
    computed="paper is a dictionary and gives estimates/conjectures; detailed spectra and mixings are deferred/inherited",
    claimed="complete translation and all predictions with experimental status",
    detail="Expected reproducibility fail.",
)
v.record(
    "zero free parameters is supported by this paper alone",
    False,
    computed="several key entries are conjectures or physical identifications, and the Higgs VEV formula is unresolved",
    claimed="zero free parameters in the fundamental equations",
    detail="Expected status fail.",
)

sys.exit(v.summary())
