#!/usr/bin/env python3
"""verify_P128.py -- Addendum 128: C_F = 4/3 from Peirce data."""

from __future__ import annotations

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("P128 -- Fundamental Casimir")
ROOT = Path(__file__).resolve().parents[1]
TEX = (ROOT / "128_Addendum_FundamentalCasimir.tex").read_text()

tf = 0.5
dim_adj_su3 = 3**2 - 1
dim_fund_su3 = 3
cf_from_index = tf * dim_adj_su3 / dim_fund_su3
cf_standard = (3**2 - 1) / (2 * 3)
ca = 3.0
nf = 5.0
b1 = (34.0 / 3.0) * ca**2 - (20.0 / 3.0) * ca * tf * nf - 4.0 * cf_from_index * tf * nf

v.record("TeX source is present", "C_F = 4/3" in TEX and "Peirce Adjoint" in TEX)
v.check("SU(3) adjoint dimension", dim_adj_su3, 8, rel=0)
v.check("SU(3) fundamental dimension", dim_fund_su3, 3, rel=0)
v.check("T_F imported value", tf, 0.5, rel=0)
v.check("Casimir-index dimension formula gives C_F", cf_from_index, 4.0 / 3.0, rel=1e-15)
v.check("standard SU(3) C_F formula", cf_standard, 4.0 / 3.0, rel=1e-15)
v.check("C_F residual", cf_from_index - cf_standard, 0.0, abs_tol=0.0)
v.check("two-loop b1 from displayed Casimir triple for nf=5", b1, 116.0 / 3.0, rel=1e-15)
v.record(
    "paper explicitly uses the representation identity C_R dim(R)=T_R dim(adj)",
    "C_R \\cdot \\dim(R)" in TEX and "T_R \\cdot \\dim(\\mathrm{adj})" in TEX,
    computed="general Casimir-index identity found",
    claimed="dimension relation used for C_F",
)

v.record(
    "eight imaginary octonion directions exist",
    False,
    computed="Im(O) has real dimension 7; O has real dimension 8",
    claimed="the 8 gluon generators are the 8-dimensional space of imaginary octonions",
    detail="Expected dimension fail inherited from P126.",
)
v.record(
    "Peirce O dimension is the SU(3) adjoint vector space",
    False,
    computed="a Peirce off-diagonal cell is an 8-real-dimensional O module; su(3) is an 8-dimensional Lie algebra of derivations in g2, not that cell itself",
    claimed="dim(adj)=8 follows because P_ij is isomorphic to O",
    detail="Expected representation-identification fail.",
)
v.record(
    "rank J3(O) derives the SU(3) fundamental representation",
    False,
    computed="rank three supplies three Peirce idempotents, but the SU(3) fundamental action on color triplets is an additional representation identification",
    claimed="dim(fund)=3 follows directly from the rank of J3(O)",
    detail="Expected derivation-status fail.",
)
v.record(
    "derivation avoids standard group-theory formulas",
    False,
    computed="the proof uses the standard representation identity C_R dim(R)=T_R dim(adj) and the standard SU(3) dimensions",
    claimed="without invoking the SU(N) formula or group theory by hand",
    detail="Expected independence/status fail.",
)
v.record(
    "T_F input is independently established inside P128",
    False,
    computed="P128 imports T_F=1/2 from P126; the P126 verifier flags the Peirce-generator identification used there",
    claimed="all three factors are independently TOE-native in this addendum",
    detail="Expected dependency fail.",
)
v.record(
    "C_A=3 is fully grounded at this stage",
    False,
    computed="P128 itself lists a more explicit G2-root derivation of C_A=3 as an open item",
    claimed="the complete Casimir triple is fully grounded after C_F",
    detail="Expected closure-status fail.",
)
v.record(
    "Hurwitz theorem imposes associativity of Peirce cells",
    False,
    computed="Hurwitz normed division algebras include the non-associative octonions; Jordan products are power-associative, not globally associative octonion multiplication",
    claimed="no other choice is consistent with associativity requirements on the Peirce cells",
    detail="Expected algebra wording fail.",
)
v.record(
    "b1 is fully first-principles closed by P128",
    False,
    computed="b1 arithmetic is correct, but it still depends on inherited C_A/T_F derivation gaps and standard representation identities",
    claimed="b1=116/3 is fully grounded with no inserted Casimir values",
    detail="Expected proof-status fail.",
)

sys.exit(v.summary())
