#!/usr/bin/env python3
"""
verify_P117.py -- Addendum 117: SU(3) breaking in vector meson f_V^2.

This verifier checks P117's charge factors, Peirce baseline couplings,
MU strange-quark correction, phi and K* arithmetic, and the omega gap
status.

The phi/K* arithmetic mostly reproduces. Flagged issues are local sign/formula
and proof-status problems: the rho residual sign is opposite the TOE-minus-PDG
convention, the summary proof writes d_P*pi/C_rho^2=8*pi although that literal
formula gives 16*pi, the omega gap interpreted as |epsilon|^2 gives a very
large amplitude rather than a standard small rho-omega mixing parameter, and
the MU-to-coupling-amplitude rule is asserted rather than derived here.
"""

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 Verifier, CheckResult


class ModernVerifier(Verifier):
    """Local adapter: modern check-line output format. Tolerance logic is
    inherited unchanged from verify_common.Verifier; only printing differs.
    Computed/claimed values stay as indented info lines."""

    def __init__(self, name: str) -> None:
        self.name = name
        self.results = []
        print(name)

    def record(self, label, ok, computed="", claimed="", detail=""):
        self.results.append(CheckResult(label, ok, computed, claimed, detail))
        status = "PASS" if ok else "FAIL"
        desc = f"{label} -- {detail}" if detail else label
        print(f"  [{status}] {len(self.results):>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"          computed: {computed}")
            print(f"          claimed : {claimed}")
        return ok

    def summary(self) -> int:
        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("P117 -- SU3 Breaking in fV2")

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

PI = math.pi
MU0 = 4.0 * PI**3 + PI**2 + PI
MU1 = 16.0 * PI**3 / 5.0 + 3.0 * PI**2 / 4.0 + 2.0 * PI / 3.0
MU = MU1 / MU0

PDG_RHO = 24.56
PDG_OMEGA = 290.97
PDG_PHI = 179.1

C_RHO2 = ((2.0 / 3.0 - (-1.0 / 3.0)) / math.sqrt(2.0)) ** 2
C_OMEGA2 = ((2.0 / 3.0 + (-1.0 / 3.0)) / math.sqrt(2.0)) ** 2
C_PHI2 = (-1.0 / 3.0) ** 2

D_P = 8.0
BARE_J1 = D_P * PI / 2.0
F_RHO = BARE_J1 / C_RHO2
F_OMEGA = BARE_J1 / C_OMEGA2
F_PHI = 36.0 * PI / MU**2
F_KSTAR = 36.0 * PI / MU
OMEGA_GAP_REL_EXP = 100.0 * (F_OMEGA - PDG_OMEGA) / PDG_OMEGA
OMEGA_GAP_REL_SU3 = 100.0 * (PDG_OMEGA - F_OMEGA) / F_OMEGA
EPSILON_SQUARED_FROM_GAP = (PDG_OMEGA - F_OMEGA) / F_OMEGA
EPSILON_FROM_GAP = math.sqrt(EPSILON_SQUARED_FROM_GAP)
RHO_RESIDUAL_TOE_MINUS = 100.0 * (F_RHO - PDG_RHO) / PDG_RHO
PHI_RESIDUAL = 100.0 * (F_PHI - PDG_PHI) / PDG_PHI


v.check("mu0", MU0, 137.036, rel=3e-6)
v.check("mu1", MU1, 108.717, rel=3e-6)
v.check("MU", MU, 0.79334, rel=3e-6)
v.check("MU^2", MU**2, 0.62939, rel=4e-6)
v.check("1/MU^2", 1.0 / MU**2, 1.589, rel=4e-4)
v.check("rho charge factor squared", C_RHO2, 1.0 / 2.0, rel=1e-12)
v.check("omega charge factor squared", C_OMEGA2, 1.0 / 18.0, rel=1e-12)
v.check("phi charge factor squared", C_PHI2, 1.0 / 9.0, rel=1e-12)
v.check("bare J1 coupling dP*pi/2", BARE_J1, 4.0 * PI, rel=1e-12)
v.check("rho f_V^2", F_RHO, 8.0 * PI, rel=1e-12)
v.check("omega f_V^2 SU3", F_OMEGA, 72.0 * PI, rel=1e-12)
v.check("omega gap relative to exp percent", OMEGA_GAP_REL_EXP, -22.3, rel=2e-3)
v.check("omega gap relative to SU3 percent", OMEGA_GAP_REL_SU3, 28.6, rel=2e-3)
v.check("phi f_V^2", F_PHI, 179.7, rel=7e-4)
v.check("phi residual percent", PHI_RESIDUAL, 0.36, rel=1e-1)
v.check("K* f_V^2 one-strange estimate", F_KSTAR, 142.6, rel=3e-4)
v.check("charge factor times Peirce factor from rho to phi", 8.0 * PI * 4.5 * (1.0 / MU**2), F_PHI, rel=1e-12)

v.check(
    "rho residual sign under TOE-minus-PDG convention",
    RHO_RESIDUAL_TOE_MINUS,
    -2.3,
    rel=5e-2,
    detail="Expected fail: 8*pi is above 24.56, so TOE-minus-PDG is about +2.33%, not -2.3%.",
)
v.check(
    "literal summary proof formula dP*pi/C_rho^2",
    D_P * PI / C_RHO2,
    8.0 * PI,
    rel=1e-12,
    detail="Expected fail: the literal formula gives 16*pi; the earlier bare formula dP*pi/2 divided by C_rho^2 gives 8*pi.",
)
v.check("omega epsilon squared from gap", EPSILON_SQUARED_FROM_GAP, 0.287, rel=3e-3)
v.check(
    "omega mixing amplitude from gap is small",
    EPSILON_FROM_GAP,
    0.04,
    rel=5e-1,
    detail="Expected fail: sqrt(0.287) is about 0.535, not a small rho-omega mixing amplitude; P118 later replaces this route.",
)
v.record(
    "omega discrepancy is explicitly left open",
    "open item" in TEX and "isospin mixing" in TEX,
    computed="omega mixing caveat found",
    claimed="omega correction not closed in P117",
)
v.record(
    "MU-to-strange-coupling rule is derived from an operator calculation",
    False,
    computed="P117 assigns one 1/sqrt(MU) amplitude factor per strange quark but does not derive it from an explicit J2 electromagnetic current/operator matrix element",
    claimed="strange-quark Peirce weight correction",
    detail="Expected proof-status fail.",
)

sys.exit(v.summary())
