#!/usr/bin/env python3
"""
verify_P082.py -- Addendum 82: Complete CKM Wolfenstein parameters.

This verifier checks the explicit trigonometric formulas in
82_Addendum_CKMComplete.tex.  The main CKM construction is close numerically,
but the printed calibration value Phi ~= 0.7490 is not the value of the stated
formula, and the mismatch propagates through rho_bar, eta_bar, |Vub|, and the
unitarity-triangle table.
"""

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 adapter: tolerance logic inherited byte-identical from
    verify_common.Verifier; only the output layer is modernised."""

    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))
        n = len(self.results)
        desc = f"{label} -- {detail}" if detail else label
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"        computed: {computed}")
            print(f"        claimed : {claimed}")
        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("P082 -- Complete CKM Wolfenstein Parameterization")

PDG_LAMBDA = 0.2245
PDG_A = 0.823
PDG_RHO = 0.147
PDG_ETA = 0.343
PDG_VUB = 0.003517
PDG_J = 3.18e-5
PDG_RB = 0.374
PDG_DELTA = 65.6
PDG_ALPHA = 84.4

lam = math.sin(math.pi / 14)
lam2 = lam**2
lam3 = lam**3
c1 = math.cos(math.pi / 14)
c2 = math.cos(2 * math.pi / 14)
c5 = math.cos(5 * math.pi / 28)
A = c1**2 * c2
delta = math.pi / 3 + math.pi / 28
delta_deg = math.degrees(delta)
phi = c1 * c2 * c5
rho = 0.5 * phi * math.cos(delta)
eta = 0.5 * phi * math.sin(delta)
rb = math.hypot(rho, eta)
rt = math.hypot(1 - rho, eta)
vub = A * lam3 * rb
jarlskog = A**2 * lam**6 * eta
beta = math.degrees(math.atan2(eta, 1 - rho))
gamma = math.degrees(math.atan2(eta, rho))
alpha = 180 - beta - gamma
bad_direct_rb = rb / (A * lam3)


def residual(value: float, target: float) -> float:
    return 100 * (value - target) / target


v.check("lambda = sin(pi/14)", lam, 0.22252, rel=5e-6)
v.check("lambda^2", lam2, 0.04952, rel=1e-4)
v.check("lambda^3", lam3, 0.01101, rel=1e-3)
v.check("A = cos^2(pi/14) cos(2pi/14)", A, 0.8563, rel=1e-4)
v.check("delta = 31pi/84 in degrees", delta_deg, 66.43, rel=5e-5)

v.check("cos(pi/14)", c1, 0.97493, rel=5e-6)
v.check("cos(2pi/14)", c2, 0.90097, rel=5e-6)
v.record("cos(5pi/28) exact component", True, f"{c5:.12g}", "component of Phi")
v.check(
    "Phi = cos(pi/14) cos(2pi/14) cos(5pi/28)",
    phi,
    0.7490,
    rel=5e-4,
    detail="Expected fail: the stated formula gives about 0.743745, not 0.7490.",
)

v.check(
    "cos(delta) printed value",
    math.cos(delta),
    0.39635,
    rel=5e-4,
    detail="Expected fail: cos(31pi/84) is about 0.399892.",
)
v.check(
    "sin(delta) printed value",
    math.sin(delta),
    0.91813,
    rel=5e-4,
    detail="Expected fail: sin(31pi/84) is about 0.916562.",
)
v.check(
    "rho_bar exact formula",
    rho,
    0.14840,
    rel=5e-4,
    detail="Expected fail: recomputing the stated formula gives about 0.148709.",
)
v.check(
    "eta_bar exact formula",
    eta,
    0.34386,
    rel=5e-4,
    detail="Expected fail: recomputing the stated formula gives about 0.340844.",
)
v.check("eta/rho = tan(delta)", eta / rho, 2.291, rel=8e-4)

v.check("lambda residual percent", residual(lam, PDG_LAMBDA), -0.88, rel=5e-3)
v.check("A residual percent", residual(A, PDG_A), 4.0, rel=2e-2)
v.check(
    "rho residual percent",
    residual(rho, PDG_RHO),
    0.95,
    rel=5e-2,
    detail="Expected fail: using the stated formula gives about +1.16%, just outside 1%.",
)
v.record(
    "rho within 1 percent of PDG",
    abs(residual(rho, PDG_RHO)) <= 1.0,
    f"{residual(rho, PDG_RHO):.6g}%",
    "within 1%",
    "Expected fail: central-value residual is about +1.16%.",
)
v.check(
    "eta residual percent",
    residual(eta, PDG_ETA),
    0.26,
    rel=5e-2,
    detail="Expected fail: recomputation gives about -0.63%, not +0.26%.",
)

v.check(
    "|rho+i eta| = Phi/2",
    rb,
    0.3745,
    rel=5e-4,
    detail="Expected fail: the exact Phi gives R_b about 0.371873.",
)
v.check(
    "R_b residual percent",
    residual(rb, PDG_RB),
    0.13,
    rel=5e-2,
    detail="Expected fail: residual is about -0.57%, not +0.13%.",
)
v.check(
    "direct matching A lambda^3 R_b = Phi/2 implies R_b",
    bad_direct_rb,
    39.7,
    rel=1e-2,
    detail="The paper notices this impossible value and switches to a dimensionless-triangle interpretation.",
)

v.check(
    "|Vub| = A lambda^3 Phi/2",
    vub,
    0.003531,
    rel=5e-4,
    detail="Expected fail: the exact stated formulas give about 0.0035088.",
)
v.check(
    "|Vub| residual percent",
    residual(vub, PDG_VUB),
    0.40,
    rel=5e-2,
    detail="Expected fail: recomputation gives about -0.23%, not +0.40%.",
)
v.check("Jarlskog value", jarlskog, 3.037e-5, rel=1e-3)
v.check("Jarlskog residual percent", residual(jarlskog, PDG_J), -4.4, rel=5e-2)
v.record(
    "Jarlskog within 1 sigma of PDG",
    abs(jarlskog - PDG_J) <= 0.15e-5,
    f"{jarlskog:.12g}",
    "3.18e-5 +/- 0.15e-5",
)

v.check("Vcb formula", lam2 * c1**2 * c2, 0.04242, rel=5e-4)
v.check(
    "R_t side",
    rt,
    0.9274,
    rel=5e-4,
    detail="Expected fail: exact rho/eta give R_t about 0.91699.",
)
v.check(
    "beta angle",
    beta,
    22.02,
    rel=1e-3,
    detail="Expected fail: exact rho/eta give beta about 21.82 degrees.",
)
v.check("gamma angle from delta", gamma, 66.43, rel=5e-5)
v.check(
    "gamma value in unitarity-triangle section",
    gamma,
    66.69,
    rel=5e-4,
    detail="Expected fail: gamma equals delta = 66.43 degrees for the paper's own formulas.",
)
v.check(
    "alpha angle",
    alpha,
    91.29,
    rel=1e-3,
    detail="Expected fail: exact beta/gamma give alpha about 91.75 degrees.",
)
v.check(
    "alpha residual percent",
    residual(alpha, PDG_ALPHA),
    8.2,
    rel=3e-2,
    detail="Expected fail: exact alpha gives about +8.71%.",
)
v.check(
    "alpha discrepancy explained as 1.1 degrees",
    alpha - PDG_ALPHA,
    1.1,
    rel=5e-2,
    detail="Expected fail: the actual alpha-PDG gap is about 7.35 degrees.",
)

sys.exit(v.summary())
