#!/usr/bin/env python3
"""
verify_P115.py -- Addendum 115: first-principles derivation of Z_mu.

This verifier checks the Z_mu arithmetic and flags proof-status issues in the
claimed Peirce-sector derivation of the cosine walk.
"""

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("P115 -- Z_mu First Principles")

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

PI = math.pi
MU0 = 4.0 * PI**3 + PI**2 + PI
ALPHA = 1.0 / MU0
LAMBDA = math.sin(PI / 14.0)
A_LO = math.cos(PI / 14.0) ** 2 * math.cos(2.0 * PI / 14.0)
NLO_FACTOR = 1.0 - 4.0 * LAMBDA**2 / 5.0
A_NLO = A_LO * NLO_FACTOR
Z_MU = 1.0 + ALPHA * A_NLO
PDG_RATIO = 206.7682830
P113_ROUNDED_LO = 208.010
EXACT_MU_LO = 208.0161


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


v.check("lambda", LAMBDA, 0.22252, rel=5e-5)
v.check("cos(pi/14)", math.cos(PI / 14.0), 0.97493, rel=4e-6)
v.check("cos(2pi/14)", math.cos(2.0 * PI / 14.0), 0.90097, rel=5e-6)
v.check("A_LO", A_LO, 0.8564, rel=6e-5)
v.check("NLO factor", NLO_FACTOR, 0.96039, rel=3e-6)
v.check("A_NLO", A_NLO, 0.8224, rel=5e-5)
v.check("alpha", ALPHA, 0.007297, rel=5e-5)
v.check("Z_mu", Z_MU, 1.006002, rel=5e-7)
v.check("rounded-route mu/e ratio", P113_ROUNDED_LO / Z_MU, 206.769, rel=3e-7)
v.check("rounded-route residual percent", pct(P113_ROUNDED_LO / Z_MU, PDG_RATIO), 0.0005, rel=3e-1)
v.check("exact-MU route mu/e ratio", EXACT_MU_LO / Z_MU, 206.775, rel=8e-7)
v.check("exact-MU route residual percent", pct(EXACT_MU_LO / Z_MU, PDG_RATIO), 0.0033, rel=3e-2)
v.record(
    "two-loop correction is explicitly left open",
    "Two-loop EM correction" in TEX and "O(\\alpha^2)" in TEX,
    computed="open item found",
    claimed="NNLO EM correction remains future work",
)
v.record(
    "P13/P23 tau-sector unification remains open",
    "P_{13}" in TEX and "would close the lepton sector" in TEX,
    computed="open item found",
    claimed="tau Peirce-sector interpretation not yet derived",
)

v.record(
    "pi/14 is the G2 minimal Weyl-chamber angle",
    False,
    computed="the G2 rank-2 Weyl chamber angle is pi/6; pi/14 is the Cabibbo angle used elsewhere, not the literal G2 Weyl chamber width",
    claimed="G2 minimal Weyl angle theta_C=pi/14",
    detail="Expected group-geometry fail.",
)
v.record(
    "two-vertex Peirce cosine walk is derived from an operator calculation",
    False,
    computed="the vertex and propagator cosine factors are assigned by geometric analogy; no Peirce EM current, propagator, or loop integral is computed",
    claimed="A_LO follows from a first-principles P12 one-loop topology",
    detail="Expected proof-status fail.",
)
v.record(
    "delta m/m = alpha*A_NLO normalization is derived",
    False,
    computed="the QFT-style self-energy normalization is stated, not derived from the J3(O) action or a renormalization calculation",
    claimed="delta m_mu^EM / m_mu = alpha * A_P12",
    detail="Expected derivation fail.",
)
v.record(
    "Bryant/Wolfenstein equality proves EM universality",
    False,
    computed="A_LO numerically equals the earlier Wolfenstein expression, but the omission of the cos(5pi/28) Bryant factor and the EM/CKM identification are asserted",
    claimed="G2 universality derives A_Wolfenstein = A_P12-EM",
    detail="Expected proof-status fail.",
)
v.record(
    "charged lepton sector is fully derived from first principles",
    False,
    computed="the paper still leaves two-loop EM, quark-sector bridge corrections, and P13/P23 tau-sector interpretation open",
    claimed="charged lepton sector now fully derived",
    detail="Expected status fail.",
)
v.record(
    "electron and tau immunity are proved as exclusion theorems",
    False,
    computed="Z_e=1 is an anchor definition, and tau immunity follows from the assumed fold-map mechanism rather than an exhaustive Peirce-loop calculation",
    claimed="only the muon receives P12 EM renormalization",
    detail="Expected proof-status fail.",
)

sys.exit(v.summary())
