#!/usr/bin/env python3
"""
verify_P056.py -- Addendum 56: Weyl geodesic derivation of Yukawa angles.

This verifier checks the numerical sine/Cabibbo content and audits the proof
status in 56_Addendum_WeylGeodesicYukawa.tex.  The basic values
sin(pi/14), sin(2*pi/14), their ratios, and the dimension identity 14=dim G2
all reproduce.  The flagged issues are proof/internal-consistency problems:
the paper says a Coxeter element of order 6 lifts to order 14, lists an
8-element eigenvalue set for a 7-dimensional representation, corrects its own
"unique plane" claim to a 6-dimensional family, reverses the y=cos(theta)
ordering inequality, and leaves the minimal-step and singlet-projection inputs
open despite the headline closure language.
"""

from __future__ import annotations

import cmath
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):
    """Output adapter: modern check-line format. Tolerance logic is
    inherited unchanged from verify_common.Verifier; only printing and
    the footer differ."""

    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)
        expected = (not ok) and ("Expected" in str(detail))
        desc = f"{label} -- {detail}" if expected else label
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"       computed: {computed}")
            print(f"       claimed : {claimed}")
        if detail and not expected:
            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("P056 -- Weyl Geodesic Yukawa Angles")

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

PI = math.pi
H_G2 = 6
DIM_G2 = 14
N_IMO = 7
THETA_C = PI / 14
WEYL_CHAMBER = PI / 6
PDG_VUS = 0.22431

y1 = math.sin(PI / 14)
y2 = math.sin(2 * PI / 14)
y3 = math.sin(7 * PI / 14)
theta1 = PI / 2 - PI / 14
theta2 = PI / 2 - 2 * PI / 14


def deg(x: float) -> float:
    return math.degrees(x)


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


def rounded_complex(z: complex) -> tuple[float, float]:
    return (round(z.real, 12), round(z.imag, 12))


v.check("Coxeter number h_G2", H_G2, 6, rel=1e-12)
v.check("dimension identity dim G2 = 2(h+1)", DIM_G2, 2 * (H_G2 + 1), rel=1e-12)
v.check("dimension identity dim G2 = 2|ImO|", DIM_G2, 2 * N_IMO, rel=1e-12)
v.check("Cabibbo angle pi/14 degrees", deg(THETA_C), 12.857, rel=3e-5)
v.check("theta_C / Weyl chamber width", THETA_C / WEYL_CHAMBER, 3 / 7, rel=1e-12)

v.check("y1=sin(pi/14)", y1, 0.22252, rel=5e-5)
v.check("y2=sin(2pi/14)", y2, 0.43388, rel=1e-5)
v.check("y3=sin(7pi/14)", y3, 1.0, rel=1e-12)
v.check("Cabibbo residual percent", pct(y1, PDG_VUS), -0.80, rel=5e-3)
v.check("y1/y2", y1 / y2, 0.5129, rel=1e-4)
v.check("y2/y1", y2 / y1, 1.9500, rel=1e-4)
v.check("double-angle ratio 2cos(pi/14)", y2 / y1, 2 * math.cos(PI / 14), rel=1e-12)
v.check("theta1 from e7 axis", theta1, 3 * PI / 7, rel=1e-12)
v.check("theta2 from e7 axis", theta2, 5 * PI / 14, rel=1e-12)
v.check("cos(theta1)=sin(pi/14)", math.cos(theta1), y1, rel=1e-12)
v.check("cos(theta2)=sin(2pi/14)", math.cos(theta2), y2, rel=1e-12)
v.check("moduli arithmetic 8-3-2+1", 8 - 3 - 2 + 1, 4, rel=1e-12)

claimed_eigen_indices = [0, 1, -1, 2, -2, 5, -5, 7]
claimed_eigenvalues = {
    rounded_complex(cmath.exp(2j * PI * k / 14)) for k in claimed_eigen_indices
}
v.record(
    "claimed order-14 eigenvalue list has dimension 7",
    len(claimed_eigenvalues) == 7,
    computed=f"{len(claimed_eigenvalues)} distinct eigenvalues from k={claimed_eigen_indices}",
    claimed="7-dimensional complexified representation",
    detail="Expected fail: the displayed 14th-root set has 8 distinct entries, not 7.",
)
v.record(
    "Coxeter element of order 6 can lift to order 14 in the same representation",
    False,
    computed="a representation image of an order-6 element has order dividing 6",
    claimed="Coxeter element has order 6 on the real representation and lifts to order 14 on V7 tensor C",
    detail="Expected proof-audit fail: an order-14 lift needs an additional construction, not the same Coxeter element.",
)
v.record(
    "14th-root eigenvalue proposition is consistent with the proof's 7th roots",
    "e^{2\\pi i k / 14}" not in TEX or "e^{2\\pi i k / 7}" not in TEX,
    computed="proposition displays 14th roots, proof says the eigenvalues are 7th roots",
    claimed="single eigenvalue description",
    detail="Expected internal-consistency fail.",
)
v.record(
    "unique associative 3-plane through e7 is proved",
    "Uniqueness is \\emph{not} absolute" not in TEX,
    computed="paper states the family through e7 has dimension 6",
    claimed="fold-map anchor forces the unique associative 3-plane",
    detail="Expected proof-audit fail: the lemma title/claim is corrected inside its own proof.",
)
v.record(
    "strict imaginary associativity gives nonzero y1,y2",
    "giving $y_1 = y_2 = 0$" not in TEX,
    computed="paper's corrected associativity derivation gives y1=y2=0",
    claimed="associativity plus anchor derives sin(pi/14), sin(2pi/14)",
    detail="Expected proof-audit fail: nonzero values require the later SU(3) singlet-projection reinterpretation.",
)
v.record(
    "ordering condition for y=cos(theta) is written correctly",
    False,
    computed=f"chosen values have theta1={theta1:.6f} > theta2={theta2:.6f} while y1={y1:.6f} < y2={y2:.6f}",
    claimed="paper says y1<y2 with y=cos(theta) requires 0<theta1<theta2<pi/2",
    detail="Expected fail: cosine is decreasing on [0, pi/2], so the inequality direction is reversed.",
)
v.check(
    "k=1,2,7 angles fit within two Weyl chambers",
    7 * THETA_C,
    2 * WEYL_CHAMBER,
    rel=1e-12,
    detail="Expected fail: the third angle is pi/2, which is not within 2*pi/6=pi/3.",
)
v.record(
    "minimal-step placement is derived from a variational principle",
    "Minimal-step variational derivation (no postulate)" not in TEX and "Open" not in TEX,
    computed="status table marks the no-postulate minimal-step derivation open",
    claimed="Weyl geodesic uniqueness proves k=1,2,7",
    detail="Expected status fail: the paper itself calls hypothesis (iii) the minimal-coupling postulate.",
)
v.record(
    "precise VEV-overlap/singlet projection is closed",
    "Precise form of VEV-overlap as" not in TEX and "Open" not in TEX,
    computed="status table marks the SU(3) singlet-projection formulation open",
    claimed="VEV-overlap formula fully specified",
    detail="Expected status fail: this is the mechanism needed after the strict associativity route gives y1=y2=0.",
)
v.record(
    "PMNS compatibility is closed in this addendum",
    "Compatibility with PMNS mixing angles" not in TEX and "Open" not in TEX,
    computed="status table marks PMNS compatibility open",
    claimed="joint structure fixes all four parameters",
    detail="Expected status fail: the text explicitly assigns this compatibility check to future work.",
)

sys.exit(v.summary())
