#!/usr/bin/env python3
"""
verify_P043.py -- Addendum 43: CFP-Noether sector charges.

This verifier checks the sector-energy identities, half-volume identities,
bulk/calibration/kinetic-density integrals, Weinberg angle arithmetic, and key
representation-theory proof claims in 43_Addendum_CFPNoether.tex.

The arithmetic backbone is mostly correct: Ee, Eb, EB, mu0, sector fractions,
the tree Weinberg angle, kinetic-vs-monad density integrals, and per-generator
charge values reproduce. The flagged issues are proof-status problems: the edge
"fiber length = 2*pi*x" statement is not the standard Hopf fiber length on unit
S3, H1 remains a monad-current definition rather than a Noether-current
derivation, irreducibility of the adjoint representation does not imply
transitivity on the whole unit sphere for G2, and the stated C2(7)=6 conflicts
with the quoted Dynkin index I(7)=1 under the usual dimension relation.
"""

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):
    """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("P043 -- CFP-Noether Sector Charges")

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

PI = math.pi
E_E = PI
E_BDY = PI**2
E_BULK = 4.0 * PI**3
MU0 = E_E + E_BDY + E_BULK


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


v.check("edge sector integral", 2.0 * PI / 2.0, PI, rel=1e-12)
v.check("boundary sector integral", 3.0 * PI**2 / 3.0, PI**2, rel=1e-12)
v.check("bulk sector integral", 16.0 * PI**3 / 4.0, 4.0 * PI**3, rel=1e-12)
v.check("mu0 sector sum", MU0, 137.036303776, rel=2e-12)
v.check("half volume S1", 0.5 * 2.0 * PI, PI, rel=1e-12)
v.check("half volume S3", 0.5 * 2.0 * PI**2, PI**2, rel=1e-12)
v.check("bulk 8pi Vol(B4)", 8.0 * PI * (PI**2 / 2.0), 4.0 * PI**3, rel=1e-12)
v.check("bulk 15/4 Vol(S6)", (15.0 / 4.0) * (16.0 * PI**3 / 15.0), 4.0 * PI**3, rel=1e-12)
v.check("G2 Casimir trace scalar", -14.0 / 7.0, -2.0, rel=1e-12)
v.check("kinetic density integral", 4.0 * PI**2 / 6.0, 2.0 * PI**2 / 3.0, rel=1e-12)
v.check("calibration density integral", PI**2 / 2.0, 4.935, rel=5e-5)
v.check("monad density integral", E_BULK, 124.025, rel=1e-6)
v.check("monad/calibration integral ratio", E_BULK / (PI**2 / 2.0), 8.0 * PI, rel=1e-12)
v.check("monad/kinetic integral ratio", E_BULK / (2.0 * PI**2 / 3.0), 6.0 * PI, rel=1e-12)
v.check("U1 generator charge", E_E / 1.0, 3.142, rel=2e-4)
v.check("SU2 per-generator charge", E_BDY / 3.0, 3.290, rel=2e-4)
v.check("G2 per-generator charge", E_BULK / 14.0, 8.859, rel=2e-5)
v.check("tree Weinberg angle 1/(1+pi)", 1.0 / (1.0 + PI), 0.2415, rel=2e-4)

frac_e = E_E / MU0
frac_b = E_BDY / MU0
v.check("Weinberg from sector fractions", frac_e / (frac_e + frac_b), 1.0 / (1.0 + PI), rel=1e-12)

v.record(
    "edge Hopf fiber length is 2*pi*x on unit S3",
    False,
    computed="for the standard unit Hopf fibration, the fiber circle length is constant up to metric normalization; 2*pi*x is the chosen sector density factor, not the literal fiber length",
    claimed="Vol(S1_x)=2*pi*x",
    detail="Expected geometric-proof fail.",
)
v.record(
    "H1 follows from standard Noether current geometry",
    False,
    computed="the paper explicitly says the monad density/current identification is a TOE definition and that the kinetic Killing-norm current gives 4*pi^2*x^5 instead",
    claimed="CFP-Noether proved as standard Noether charge equality",
    detail="Expected proof-status fail.",
)
v.record(
    "irreducible adjoint action implies transitivity on unit sphere for G2",
    False,
    computed="irreducibility does not generally imply transitivity on the unit sphere; G2 adjoint orbits have additional invariants/rank structure",
    claimed="all G2 generators in a unit sphere are related by adjoint action",
    detail="Expected representation-theory fail. The SU(2) adjoint case is special.",
)

dynkin_index = 1.0
dim_g2 = 14.0
dim_7 = 7.0
c2_from_index = dynkin_index * dim_g2 / dim_7
v.check(
    "C2(7) from Dynkin index I(7)=1",
    c2_from_index,
    6.0,
    rel=1e-3,
    detail="Expected fail: the dimension relation C2(R) dim R = I(R) dim g gives C2(7)=2 if I(7)=1, not 6.",
)
v.record(
    "bulk CFP-Noether has no open item beyond already-proved geometry",
    False,
    computed="the bulk result is conditional on monad normalization mu0=alpha^-1, and the text says this is the deepest open layer not addressed here",
    claimed="CFP is no longer a postulate / unconditional given mu0",
    detail="Expected status fail when read as an unconditional closure of the full proof chain.",
)
v.record(
    "A42 one-loop running to sin2thetaW(MZ)=0.2312 is verified in P43",
    False,
    computed="P43 imports A42 Theorem 5.2 and does not recompute the RG running; P42 verifier flagged the running/normalization issue separately",
    claimed="combined with one-loop SM running follows with no free parameters",
    detail="Expected reproducibility/status fail.",
)

sys.exit(v.summary())
