#!/usr/bin/env python3
"""verify_P175.py -- Addendum 175: delta_QLC from G2/A2 weights."""

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("P175 -- delta QLC")
ROOT = Path(__file__).resolve().parents[1]
TEX = (ROOT / "175_Addendum_deltaQLC_WeightLattice.tex").read_text()

PI = math.pi
lam = math.sin(PI / 14.0)
lam4 = lam**4
primary = (3.0 / 5.0) * lam4
alternative = (7.0 / 12.0) * lam4
fit = 0.00147

w1 = (2.0 / math.sqrt(6.0), -1.0 / math.sqrt(6.0), -1.0 / math.sqrt(6.0))
wb2 = (1.0 / math.sqrt(6.0), -2.0 / math.sqrt(6.0), 1.0 / math.sqrt(6.0))
dot = sum(a * b for a, b in zip(w1, wb2))
angle = math.acos(dot)

v.record("TeX source is present", "delta_{\\mathrm{QLC}}" in TEX and "3}{5}" in TEX)
v.check("theta_C radians", PI / 14.0, 0.22440, rel=3e-5)
v.check("theta_C degrees", 180.0 / 14.0, 12.857, rel=3e-5)
v.check("lambda", lam, 0.222520934, rel=2e-9)
v.check("lambda^4", lam4, 2.4518e-3, rel=3e-5)
v.check("QLC exact solar angle", PI / 4.0 - PI / 14.0, 5.0 * PI / 28.0, rel=1e-15)
v.check("QLC exact solar angle degrees", (5.0 * PI / 28.0) * 180.0 / PI, 32.143, rel=8e-6)
v.check("adjacent 3/3bar weight angle", angle, PI / 3.0, rel=1e-15)
v.check("gross QLC mismatch", PI / 3.0 - PI / 4.0, PI / 12.0, rel=1e-15)
v.check("root mismatch", PI / 6.0 - PI / 14.0, 2.0 * PI / 21.0, rel=1e-15)
v.check("off-Cartan dimension formula", 7 - 2, 5, rel=0)
v.check("primary coefficient", 3.0 / (7.0 - 2.0), 3.0 / 5.0, rel=0)
v.check("primary delta_QLC", primary, 1.4711e-3, rel=8e-5)
v.check("primary fractional error vs fitted value", abs(primary - fit) / fit, 0.0007, rel=8e-2)
v.check("alternative delta_QLC", alternative, 1.4302e-3, rel=8e-5)
v.check("alternative fractional error vs fitted value", abs(alternative - fit) / fit, 0.0271, rel=2e-2)
v.record(
    "first-principles caveat is present",
    "does not constitute a first-principles proof" in TEX and "Addendum~176" in TEX,
    computed="open-status caveat found",
    claimed="3/5 coefficient remains open",
)

v.record(
    "off-Cartan dimension equals the nonzero weight-space dimension",
    False,
    computed="the 7-representation has one zero weight and six nonzero weights; subtracting rank(G2)=2 gives 5, which is a different count",
    claimed="dim W_perp=dim V7-rank(G2)=5 is the total off-Cartan weight-space dimension",
    detail="Expected dimension-interpretation fail.",
)
v.record(
    "lepton sector contributes only two weight directions",
    False,
    computed="under 7 -> 1+3+3bar, both triplet sectors have dimension 3; the 3+2 split is an extra QLC-frame convention",
    claimed="the quark sector contributes three and the lepton sector contributes the remaining two",
    detail="Expected representation-count fail.",
)
v.record(
    "gross mismatch Delta is used in the final formula",
    False,
    computed="the theorem formula is (3/5)*lambda^4 and contains no factor of Delta=pi/12",
    claimed="delta_QLC is the fourth-order projection of the gross lattice mismatch Delta",
    detail="Expected derivation gap.",
)
v.record(
    "lambda^4 order selection is derived from a QLC operator",
    False,
    computed="the paper gives Wolfenstein-order motivation, but no rotation operator or projection calculation deriving the fourth-order coefficient",
    claimed="the Wolfenstein expansion maps the lattice displacement to lambda^4",
    detail="Expected operator/proof gap.",
)
v.record(
    "3/5 coefficient is uniquely forced",
    False,
    computed="the text explicitly says exact 3/5 is not proved and the rigorous derivation is deferred to Addendum 176",
    claimed="closed-form algebraic expression with no independently fitted parameters",
    detail="Expected status fail.",
)
v.record(
    "P175 removes the fitted-parameter status of delta_QLC",
    False,
    computed="the value matches the P74 fit closely, but the coefficient choice is still justified by a plausible dimensional argument plus numerical agreement",
    claimed="delta_QLC is now derived rather than fitted",
    detail="Expected closure-status fail.",
)

sys.exit(v.summary())
