#!/usr/bin/env python3
"""
verify_P075.py -- Addendum 75: Down-Type Yukawa Matrix.

This verifier checks the numerical Wolfenstein-A and |V_cb| claims in
75_Addendum_DownYukawa.tex.

Most numerical evaluations in the final table are consistent. The audit flags
the paper's internal narrative inconsistency: the abstract/early theorem claim
the simpler y1*y2^2 formula as the proved closure, while the later theorem and
comparison table replace it with the trilinear formula
sin^2(pi/14) cos^2(pi/14) cos(2pi/14). The two differ by about 1.2%, so they
cannot both be the exact derived formula. The paper also states sigma offsets
that do not match the stated PDG uncertainty, and later admits one structural
G2 input was used without a full proof.
"""

import math
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from verify_common import Verifier as _BaseVerifier, CheckResult


class Verifier(_BaseVerifier):
    """Output-layer normalization only: same checks, modern [PASS]/[FAIL] format."""

    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)
        note, info = "", detail
        if not ok and "Expected" in detail:
            i = detail.find("Expected")
            note = " -- " + detail[i:]
            info = detail[:i].rstrip().rstrip(";")
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {label}{note}")
        if computed != "" or claimed != "":
            print(f"        computed: {computed}")
            print(f"        claimed : {claimed}")
        if info:
            print(f"        {info}")
        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 = Verifier("P075 -- Down-Type Yukawa / Wolfenstein A")

LAM = math.sin(math.pi / 14)
C1 = math.cos(math.pi / 14)
C2 = math.cos(2 * math.pi / 14)
Y2 = math.sin(2 * math.pi / 14)

VCB_PDG = 0.04110
VCB_SIGMA = 0.0014
A_PDG = 0.823
A_SIGMA = 0.015

vcb_alt = LAM * Y2**2
a_alt = Y2**2 / LAM
vcb_exact = LAM**2 * C1**2 * C2
a_exact = C1**2 * C2

v.check("lambda = sin(pi/14)", LAM, 0.22252, rel=5e-6)
v.check("y2 = sin(2pi/14)", Y2, 0.43388, rel=1e-5)
v.check("cos(2pi/14)", C2, 0.90097, rel=5e-6)
v.check("lambda^2", LAM**2, 0.0495156, rel=1e-5)

v.check("alternative |V_cb|' = sin(pi/14) sin^2(2pi/14)", vcb_alt, 0.04189, rel=2e-4)
v.check("alternative A' = sin^2(2pi/14)/sin(pi/14)", a_alt, 0.8460, rel=2e-4)
v.check("alternative A' identity = 4 sin(pi/14) cos^2(pi/14)", 4 * LAM * C1**2, a_alt, rel=1e-12)
v.check("alternative |V_cb|' residual percent", 100 * (vcb_alt - VCB_PDG) / VCB_PDG, 1.9, rel=2e-2)
v.check("alternative A' residual percent", 100 * (a_alt - A_PDG) / A_PDG, 2.8, rel=2e-2)

v.check(
    "exact trilinear |V_cb| = sin^2(pi/14) cos^2(pi/14) cos(2pi/14)",
    vcb_exact,
    0.04242,
    rel=5e-4,
)
v.check("exact trilinear A = cos^2(pi/14) cos(2pi/14)", a_exact, 0.8563, rel=2e-4)
v.check("exact |V_cb| residual percent", 100 * (vcb_exact - VCB_PDG) / VCB_PDG, 3.2, rel=3e-2)
v.check("exact A residual percent", 100 * (a_exact - A_PDG) / A_PDG, 4.0, rel=3e-2)

v.check(
    "abstract/early theorem |V_cb| formula equals final exact formula",
    vcb_alt,
    vcb_exact,
    rel=1e-5,
    detail=(
        "Expected fail: the early y1*y2^2 formula and final trilinear formula "
        "differ by about 1.2%."
    ),
)
v.check(
    "abstract/early theorem A formula equals final exact formula",
    a_alt,
    a_exact,
    rel=1e-5,
    detail=(
        "Expected fail: the paper presents A'=0.8460 early and A=0.8563 later "
        "as the structural closure."
    ),
)
v.check(
    "two expressions are equivalent",
    (a_exact - a_alt) / a_exact,
    0.0,
    abs_tol=1e-6,
    detail="Expected fail: the paper's own values differ by about 1.21%.",
)

v.check(
    "relative difference exact-vs-alternative A",
    100 * (a_exact - a_alt) / a_exact,
    1.2,
    rel=1e-2,
)
v.check(
    "exact A sigma from PDG central",
    (a_exact - A_PDG) / A_SIGMA,
    2.7,
    rel=5e-2,
    detail="Expected fail: using A_obs=0.823 +/- 0.015 gives about 2.22 sigma.",
)
v.check(
    "exact A sigma from PDG upper edge",
    (a_exact - (A_PDG + A_SIGMA)) / A_SIGMA,
    1.5,
    rel=5e-2,
    detail="Expected fail: using the stated upper edge 0.838 gives about 1.22 sigma.",
)
v.check("exact |V_cb| sigma from PDG central", (vcb_exact - VCB_PDG) / VCB_SIGMA, 0.94, rel=2e-2)

v.record(
    "Phase 5e structural proof complete",
    False,
    computed="key G2 holonomy input explicitly left without full proof",
    claimed="Phase 5e is closed from first principles",
    detail=(
        "Expected fail: the paper later says varphi(e7,v_perp,w_perp)=sin(pi/14) "
        "was used without a full proof."
    ),
)

sys.exit(v.summary())
