#!/usr/bin/env python3
"""verify_i.py -- Paper i: cover paper / ternary framing."""

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 adapter: identical tolerance logic, modern output 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)
        desc, info = label, detail
        i = detail.find("Expected")
        if i >= 0:
            desc = f"{label} -- {detail[i:]}"
            info = detail[:i].rstrip().rstrip(";")
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"        computed: {computed}")
            print(f"        claimed : {claimed}")
        if info:
            print(f"        {info}")
        return bool(ok)

    def summary(self) -> int:
        passed = sum(bool(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("Paper i -- Cover Paper")
ROOT = Path(__file__).resolve().parents[2]
TEX = (ROOT / "toe" / "i_Paper_I.tex").read_text()

PI = math.pi
omega = 4.0 * PI**3 + PI**2 + PI

v.record("TeX source is present", "Paper i: I" in TEX and "Cover Paper" in TEX)
v.check("balanced ternary identity 1 + T = 0", 1 + (-1), 0, abs_tol=0)
v.check("monad omega arithmetic", omega, 137.03630377587825, abs_tol=2e-13)
v.check("alpha inverse rounded value", omega, 137.036, rel=3e-6)
v.check("three harmonic contribution count", len([PI, PI**2, 4 * PI**3]), 3, rel=0)
v.record(
    "Paper i is explicitly outside the numbered corpus",
    "stands outside the numbered corpus" in TEX and "Read this paper before Paper~00" in TEX,
    computed="scope statement found",
    claimed="cover/framing paper",
)
v.record(
    "numbered corpus scope list matches papers 00--34",
    "Papers~00--20" in TEX and "Papers~21--26" in TEX and "Papers~27--31" in TEX and "Papers~32--34" in TEX,
    computed="range list found",
    claimed="cover paper summarizes the initial numbered corpus",
)

v.record(
    "omega equals the physical fine-structure inverse exactly",
    False,
    computed=f"4*pi^3+pi^2+pi = {omega:.12f}; this is close to but not identical to CODATA alpha^-1",
    claimed="Omega = alpha^{-1}",
    detail="Expected physical-identification precision fail.",
)
v.record(
    "Paper i proves the numbered papers are true together",
    False,
    computed="Paper i gives a conceptual framing; it does not verify each theorem, formula, or dependency in the numbered papers",
    claimed="explains why the numbered papers are true together",
    detail="Expected scope/status fail.",
)
v.record(
    "balanced ternary derives the three fermion families",
    False,
    computed="the statement links three strata and three families conceptually, but no representation-theoretic or phenomenological derivation is supplied in this cover paper",
    claimed="this is why there are three fermion families",
    detail="Expected derivation gap.",
)
v.record(
    "Nullity/Self/Unity claims are code-verifiable mathematics",
    False,
    computed="these sections use philosophical language and internal terminology rather than formal definitions or falsifiable formulas",
    claimed="Self + Nullity = Unity grounds the corpus",
    detail="Expected non-formal/status fail.",
)
v.record(
    "Paper i covers the later addendum corpus through P200",
    False,
    computed="the TeX says the 35 papers that follow are Papers 00--34; it does not inventory addenda 35--200",
    claimed="cover paper for the full requested i and 0--200 audit scope",
    detail="Expected scope-limit fail.",
)

sys.exit(v.summary())
