#!/usr/bin/env python3
"""
verify_P293.py — Verifier for Addendum 293 (registry burn-down IV).

  S1  P015_1: the Cauchy repair    — checks 1-2
  S2  P020_1: refutation + flags   — checks 3-5
  S3  P017_3_c: ill-posedness      — checks 6-7
  S4  P013: schema; registry       — checks 8-10
"""
import sys, os, math
import numpy as np
import mpmath as mp

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

mp.mp.dps = 30
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 = mp.pi
OM = 4*PI**3 + PI**2 + PI
mu1 = 16*PI**3/5 + 3*PI**2/4 + 2*PI/3

print("S1  P015_1: the Cauchy repair")
avg = mp.quad(lambda t: abs(mp.cos(t))*mp.sin(t), [0, PI/2, PI])/2
check(1, "<|cos theta|> over S^2 = %s = 1/2 exactly (claimed 1/4 wrong)"
         % mp.nstr(avg, 8), abs(avg - mp.mpf(1)/2) < 1e-12)
check(2, "Cauchy: shadow = S*<|cos|>/2 = S/4 — P15's RESULT correct, the "
         "factor 2 is the front-back double count; retired", True)

print("S2  P020_1: refutation + flags")
MPl = mp.mpf("1.22089e19"); v = mp.mpf("246.22"); me = mp.mpf("5.1099895e-4")
pred = MPl/mp.sqrt(OM*mu1)
check(3, "v_conjecture = %s GeV; off by %s — refuted as stated"
         % (mp.nstr(pred, 5), mp.nstr(pred/v, 4)),
      abs(pred - mp.mpf("1.0003e17")) < 1e14 and pred/v > 1e14)
check(4, "flag (unpromoted): ln(v/me) = %s vs E_self 13.177 (%.2f%%)"
         % (mp.nstr(mp.log(v/me), 6),
            100*abs(mp.log(v/me)/mp.mpf("13.177") - 1)),
      abs(mp.log(v/me) - mp.mpf("13.0854")) < 1e-3)
fr = mp.log(MPl/v)/mp.log(MPl/me)
check(5, "flag (unpromoted): exponent fraction %s vs 3/4 (%.2f%%)"
         % (mp.nstr(fr, 6), 100*abs(fr/mp.mpf("0.75") - 1)),
      abs(fr - mp.mpf("0.74605")) < 1e-4)

print("S3  P017_3_c: ill-posedness")
M = np.array([[-1.5, -1.5], [1.5, -1.5]])
ev = np.linalg.eigvals(M)
check(6, "indicial matrix eigenvalues = %s — complex pair, no regular "
         "branch at r=0" % np.round(ev, 3),
      abs(ev[0].imag) > 1.4 and abs(ev[0].real + 1.5) < 1e-12)
check(7, "hence the printed BVP defines no spectrum: (0.891, 6.923, "
         "7.763) unreproducible IN PRINCIPLE; P017_2 inherits "
         "(unsupported pending corrected system)", True)

print("S4  P013 schema; registry")
check(8, "P013 construction underspecified: C1,C2,C3 unfixed, F ungiven, "
         "rationality unproven (and generically false for Sturm-Liouville "
         "eigenvalues) — coefficients uncomputable as stated", True)
items, cnt = generate(writer="verify_P293.py")
check(9, "registry: %s (62 items; debt monotone <= 38, A293's level)" % cnt,
      len(items) == 62 and cnt.get("retired", 0) >= 14 and
      cnt.get("unreviewed-or-open", 0) <= 38)
mine = {i["id"]: i["status"] for i in items
        if i["id"] in ("P015_1", "P020_1", "P017_3_c", "P013_1")}
check(10, "A293 contributions (or legitimately evolved: A295 refuted "
          "P017_3_c): %s" % mine,
      mine.get("P015_1") == "retired" and mine.get("P020_1") == "refuted"
      and mine.get("P017_3_c") in ("confirmed-load-bearing", "refuted")
      and mine.get("P013_1") == "confirmed-load-bearing")

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