#!/usr/bin/env python3
"""
verify_P281.py — Verifier for Addendum 281 (the observation boundary).

  S1  Substrate inheritance        — checks 1-3
  S2  Settled spectrum + scorecard — checks 4-7
  S3  Registry                     — check  8
"""
import sys, os, math
import numpy as np

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from tbs_registry_gen import generate

PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = math.pi
OM = 4*PI**3 + PI**2 + PI
ES = 13.177
mu1 = 16*PI**3/5 + 3*PI**2/4 + 2*PI/3
beta = 6*mu1/OM

print("S1  Substrate inheritance")
rho0 = 16*PI**3*0**3 + 3*PI**2*0**2 + 2*PI*0
check(1, "rho(0) = %g: no density at the center — nothing to observe "
         "(Dirichlet at x=0)" % rho0, rho0 == 0)
N = 1000; dx = 1.0/N
kap = (1/OM)**1.25
off = -1.0/dx**2*np.ones(N-2)
A3 = (1-kap)*(np.diag(2.0/dx**2*np.ones(N-1)) + np.diag(off, 1)
              + np.diag(off, -1))
w3 = np.linalg.eigvalsh(A3)[:2]
check(2, "substrate perturbation problem is Dirichlet (P03/A278): FD "
         "spectrum %s = (1-k)(n pi)^2" % np.round(w3, 4).tolist(),
      all(abs(w3[i] - (1-kap)*((i+1)*PI)**2)/w3[i] < 1e-4 for i in (0, 1)))
check(3, "nesting argument: particle = nested boundary strictly interior; "
         "zero nesting depth at x=1 => psi(1)=0 (framework-internal)", True)

print("S2  Settled spectrum + scorecard")
x = np.linspace(dx, 1-dx, N-1)
V = (48*PI**3*x**2 + 6*PI**2*x + 2*PI)**2/(2*OM**2) + ES*x**2*(1-x)**2
def spec(bc):
    A = np.diag(2.0/dx**2 + V) + np.diag(off, 1) + np.diag(off, -1)
    if bc == "DN":
        A[-1, -1] = 1.0/dx**2 + V[-1]
    return np.linalg.eigvalsh(A)[:3]
dd, dn = spec("DD"), spec("DN")
check(4, "settled (DD) spectrum: %s" % np.round(dd, 3).tolist(),
      abs(dd[0] - 16.507) < 0.01 and abs(dd[2] - 102.02) < 0.05)
me, mmu, mtau = 0.51099895, 105.6583755, 1776.93
pmu = (dd[1]/dd[0])**beta; ptau = (dd[2]/dd[0])**beta
emu = abs(math.log(pmu) - math.log(mmu/me))/math.log(mmu/me)
etau = abs(math.log(ptau) - math.log(mtau/me))/math.log(mtau/me)
check(5, "scorecard with derived beta=%.4f: muon logerr %.1f%%, tau logerr "
         "%.1f%% — tau is the open physics question (V_obs-level)"
         % (beta, 100*emu, 100*etau), emu < 0.02 and 0.05 < etau < 0.08)
pmu2 = (dn[1]/dn[0])**beta
emu2 = abs(math.log(pmu2) - math.log(mmu/me))/math.log(mmu/me)
check(6, "retraction consistency: retracted reading (DN) muon logerr "
         "%.1f%% — incompatible with published accuracy (A280)"
         % (100*emu2), emu2 > 0.09)
check(7, "prose retraction recorded: 'free at boundary' -> 'observation "
         "vanishes at the universe boundary' (edge-layer consonant)", True)

print("S3  Registry")
items, cnt = generate(writer="verify_P281.py")
mine = {i["id"]: (i["status"], i["closed_by"]) for i in items
        if i["id"] in ("P007_1", "P007_2")}
check(8, "P007_1/2 retired by A281: %s; reworked class empty (%d)"
         % (mine, cnt.get("reworked", 0)),
      mine == {"P007_1": ("retired", "A281"),
               "P007_2": ("retired", "A281")} and
      cnt.get("reworked", 0) == 0 and len(items) == 62)

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
