#!/usr/bin/env python3
"""verify_P021.py -- Paper 21: observer-dependent P vs NP framework."""

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 Verifier, CheckResult


class ModernVerifier(Verifier):
    """Local adapter: modern check-line output format. Tolerance logic is
    inherited unchanged from verify_common.Verifier; only printing differs.
    Computed/claimed values stay as indented info lines."""

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

    def summary(self) -> int:
        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("P021 -- Observer Complexity")
ROOT = Path(__file__).resolve().parents[2]
TEX = (ROOT / "toe" / "21_Paper_PequalsNP.tex").read_text()
PI = math.pi
OMEGA = 4 * PI**3 + PI**2 + PI

v.record("TeX source is present", "P versus NP" in TEX or "P vs" in TEX)
v.check("bulk fraction", 4 * PI**3 / OMEGA, 0.9053, rel=4e-4)
v.check("boundary fraction", PI**2 / OMEGA, 0.0720, rel=4e-4)
v.check("edge fraction", PI / OMEGA, 0.0229, rel=2e-3)
v.check("layer fractions sum to one", (4 * PI**3 + PI**2 + PI) / OMEGA, 1.0, rel=1e-15)
v.check("self-lensing energy quoted value", 13.1767, 13.177, rel=3e-5)
v.check("phase transition numeric comparison alpha^-1/32", OMEGA / 32.0, 4.26, rel=6e-3)
v.record(
    "scope disclaimer is present",
    "does not resolve" in TEX and "formal complexity theory" in TEX,
    computed="paper explicitly says it does not settle P vs NP",
    claimed="conceptual framework",
)

v.record(
    "single-observer P=NP is a theorem of standard complexity theory",
    False,
    computed="P and NP are Turing-machine language classes; C o P = I is not a polynomial-time search algorithm for NP witnesses",
    claimed="For a single observer, P = NP",
    detail="Expected formal-scope fail.",
)
v.record(
    "observer-dependent resolution settles the formal alternatives",
    False,
    computed="the paper reuses P/NP terminology for geometric observer layers and explicitly disclaims a Clay-problem proof",
    claimed="different observer structures yield P=NP, P subset NP, and P!=NP",
    detail="Expected terminology/scope fail.",
)
v.record(
    "hard NP-complete instance fraction is derived",
    False,
    computed="no probability model over instances or asymptotic hardness definition is supplied; 2.3% is imported from the alpha layer fraction",
    claimed="genuinely hard instances are approximately pi/alpha^-1",
    detail="Expected empirical/proof gap.",
)
v.record(
    "90.5/7.2/2.3 layer fractions classify all NP instances",
    False,
    computed="there is no map from SAT or arbitrary NP-complete instances to the geometric layer measure",
    claimed="90.5% bulk, 7.2% boundary, 2.3% edge",
    detail="Expected model-definition fail.",
)
v.record(
    "phase transition ratio follows from alpha^-1/32",
    False,
    computed=f"alpha_geom^-1/32={OMEGA/32:.6f}, whereas the 3-SAT threshold is quoted near 4.26 and depends on the random model",
    claimed="4.26 approx alpha^-1/32 suggests a geometric boundary",
    detail="Expected numerology/status fail.",
)
v.record(
    "quantum speedup ceiling for NP-complete problems is proved",
    False,
    computed="no argument in quantum query/complexity models is supplied; this is at most a conjectural alignment with known evidence",
    claimed="no quantum algorithm should achieve exponential speedup for NP-complete problems generally",
    detail="Expected proof gap.",
)
v.record(
    "self-reference implies bulk access as complexity theorem",
    False,
    computed="self-lensing energy is not translated into a computational model or complexity class inclusion",
    claimed="systems capable of asking P vs NP already operate where P=NP",
    detail="Expected formalization fail.",
)

sys.exit(v.summary())
