#!/usr/bin/env python3
"""verify_P023.py -- Paper 23: Yang-Mills mass gap framework."""

from __future__ import annotations

import math
import sys
from pathlib import Path



PASS = FAIL = 0
_N = 0

def record(label, ok, computed="", claimed="", detail=""):
    """Modern-format check line; behavior-preserving port of verify_common."""
    global PASS, FAIL, _N
    _N += 1
    ok = bool(ok)
    desc = label
    if ok:
        PASS += 1
    else:
        FAIL += 1
        if "Expected" in detail:
            i = detail.find("Expected")
            desc = f"{label} -- {detail[i:]}"
            detail = detail[:i].rstrip().rstrip(";")
    print(f"  [{'PASS' if ok else 'FAIL'}] {_N:>2}. {desc}")
    if computed != "" or claimed != "":
        print(f"        computed: {computed}")
        print(f"        claimed : {claimed}")
    if detail:
        print(f"        {detail}")
    return ok

def check(label, computed, claimed, *, rel=1e-3, abs_tol=None, detail=""):
    if abs_tol is not None:
        ok = abs(computed - claimed) <= abs_tol
        err_detail = f"abs err={abs(computed - claimed):.6g}, tol={abs_tol:.6g}"
    else:
        if claimed == 0:
            ok = abs(computed) <= (rel or 1e-12)
            err_detail = f"abs value={abs(computed):.6g}, tol={rel:.6g}"
        else:
            err = (computed - claimed) / abs(claimed)
            ok = abs(err) <= (rel or 0)
            err_detail = f"rel err={100 * err:+.6g}%, tol={100 * (rel or 0):.6g}%"
    return record(label, ok, computed, claimed, err_detail + (f"; {detail}" if detail else ""))

print("P023 -- Yang-Mills Mass Gap")
ROOT = Path(__file__).resolve().parents[2]
TEX = (ROOT / "toe" / "23_Paper_YangMillsMassGap.tex").read_text()
PI = math.pi
OMEGA = 4 * PI**3 + PI**2 + PI
fb = PI**2 / OMEGA

record("TeX source is present", "Yang-Mills Mass Gap" in TEX)
check("boundary fraction f_b", fb, 0.072022, rel=1e-5)
check("SU(2) fundamental Casimir", 0.5 * 1.5, 0.75, rel=0)
check("SU(3) fundamental Casimir", (3**2 - 1) / (2 * 3), 4 / 3, rel=1e-15)
check("Casimir approximation sqrt(3)/24", math.sqrt(3) / 24, 0.072169, rel=4e-6)
check("relative Casimir gap percent", 100 * ((math.sqrt(3) / 24 - fb) / fb), 0.20, rel=3e-2)
check("sqrt(f_b)", math.sqrt(fb), 0.2683, rel=4e-4)
check("SU(2) group factor", 2 * math.sqrt(2 / (3 / 4)), 3.27, rel=2e-3)
check("SU(3) group factor", 2 * math.sqrt(3 / (4 / 3)), 3.0, rel=1e-15)
check("QCD mass estimate", 0.805 * 7 * 250, 1.41e3, rel=2e-3)
record("Millennium proof disclaimer is present", "does not constitute a rigorous proof" in TEX)

record(
    "direct SU(2)=S3 route derives f_b rather than defines it",
    False,
    computed="the proof chooses shell boundaries by cumulative volume fractions f_e and f_b, so the equality of the boundary fraction to f_b is by construction",
    claimed="boundary fraction via SU(2) congruent S3 is exact derivation",
    detail="Expected circularity/status fail.",
)
record(
    "SU(2) is Riemannian-isometric to the unit S3 without metric normalization",
    False,
    computed="SU(2) is diffeomorphic to S3; Riemannian equality depends on the chosen normalization of the bi-invariant/Killing metric",
    claimed="SU(2) congruent S3 as a Riemannian manifold",
    detail="Expected metric-normalization gap.",
)
record(
    "minimum misalignment equals f_b is proved",
    False,
    computed="the instanton/boundary-crossing argument is qualitative; no variational lower bound on the alignment functional is proved",
    claimed="(delta gamma)_min = f_b",
    detail="Expected proof gap.",
)
record(
    "mass-gap formula is internally consistent",
    False,
    computed="the proof first gives m_gap^2=f0^2*f_b^2 and m_gap=f0*f_b, then switches to f0*sqrt(f_b)*c_G",
    claimed="m_gap=f0*sqrt(f_b)*c_G",
    detail="Expected formula inconsistency.",
)
record(
    "alignment mass term is gauge-invariant Yang-Mills construction",
    False,
    computed="a local term m^2(x)|A|^2 is not gauge invariant without gauge fixing or additional fields, despite gamma_A itself being gauge invariant",
    claimed="modified action preserves gauge symmetry",
    detail="Expected gauge-invariance fail.",
)
record(
    "reflection positivity follows from pointwise positivity",
    False,
    computed="OS reflection positivity is not guaranteed merely because an exponential factor is positive; it is a condition on reflected field correlations/measures",
    claimed="positive mass factor preserves reflection positivity",
    detail="Expected OS-proof fail.",
)
record(
    "framework proves existence of 4D quantum Yang-Mills theory",
    False,
    computed="no constructive measure, continuum limit, renormalization, or OS axiom verification is supplied",
    claimed="strictly positive mass gap for compact simple G",
    detail="Expected Millennium-scope fail.",
)

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