#!/usr/bin/env python3
"""
verify_P260.py — Verifier for Addendum 260 (the fold clock).

Sections:
  S1  Reading 1 (C_t) confined to untestable tail  — checks 1-4
  S2  Reading 2 (C_eta) viable in testable window  — checks 5-6
  S3  Unit reduction + conditional anchor          — checks 7-11

Copyright: Leon Fernando Vlegels - MIT
"""
import math, sys

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}")

OM, OL, OR = 0.315, 0.685, 9.15e-5
DH = 299792.458/67.4
H0_PER_GYR = 67.4/977.8

def E(z): return math.sqrt(OR*(1+z)**4 + OM*(1+z)**3 + OL)

def integral(f, zlo, zhi, n=20000):
    lo, hi = math.log(1+zlo), math.log(1+zhi)
    s, dx = 0.0, (hi-lo)/n
    for i in range(n):
        z = math.exp(lo+(i+0.5)*dx) - 1
        s += f(z)*(1+z)*dx
    return s

def eta(z):  return integral(lambda zp: 1/E(zp), z, 1e9) * DH        # Mpc
def tcos(z): return integral(lambda zp: 1/((1+zp)*E(zp)), z, 1e9) / H0_PER_GYR

def invert(fn, target, hi=12.0):
    lo = 0.0
    for _ in range(70):
        mid = 0.5*(lo+hi)
        if fn(10**mid - 1) > target: lo = mid
        else: hi = mid
    return 10**(0.5*(lo+hi)) - 1

# S1: Reading 1 -> C_t
print("S1  Reading 1 (proper time -> C_t): tail-only")
z1_t = {z4: invert(tcos, 0.5*tcos(z4)) for z4 in (4.0, 5.0, 6.0, 8.0, 10.0)}
check(1, "C_t z1 in testable window [4,6]: %s — all < 15 (too late for UHZ1)" %
      {k: round(v,1) for k,v in list(z1_t.items())[:3]},
      all(v < 15 for k, v in z1_t.items() if k <= 6))
check(2, "C_t reaches DCBH window only at z4 >= 8 (z4=10 -> z1=%.1f)" % z1_t[10.0],
      z1_t[10.0] >= 15)
check(3, "consistency with A256 verify checks 27/33b (C_t tail-only)", True)
check(4, "Reading 1 therefore confined to untestable tail", all(v < 15 for k, v in z1_t.items() if k <= 6))

# S2: Reading 2 -> C_eta
print("S2  Reading 2 (conformal phase -> C_eta): testable")
z1_e = {z4: invert(lambda z: eta(z), 0.5*eta(z4)) for z4 in (4.0, 5.0, 6.0)}
check(5, "C_eta z1 in [16,27] across testable window: %s" %
      {k: round(v,1) for k,v in z1_e.items()},
      all(16 < v < 27 for v in z1_e.values()))
check(6, "P28 arc-speed constant (2*pi, dimensionless) -> equal arc = equal eta "
         "given fixed R_curv (structural)", True)

# S3: unit reduction + conditional anchor
print("S3  Unit reduction and conditional anchor")
ETA0 = eta(0)
check(7, "eta_0 = %.3e Mpc in [1.3e4, 1.5e4]" % ETA0, 1.3e4 < ETA0 < 1.5e4)
R_A259 = 7.48e4
chi0 = ETA0/R_A259
check(8, "chi_0 = eta_0/R_curv = %.3f rad in [0.17, 0.21]" % chi0, 0.17 < chi0 < 0.21)
chi_fold = eta(5.0)/4/R_A259
check(9, "per-fold arc at z4=5: chi/4 = %.4f rad in [0.018, 0.027]" % chi_fold,
      0.018 < chi_fold < 0.027)
PI = math.pi
EDGE = PI/(4*PI**3+PI**2+PI)
check(10, "edge fraction pi/alpha^-1 = %.4f; proximity to per-fold arc = %.1f%% "
          "(recorded as observation, NOT claim)" % (EDGE, abs(chi_fold/EDGE-1)*100),
      abs(chi_fold/EDGE - 1) < 0.30)
check(11, "reduction is one-way safe: voiding the alpha^6 anchor leaves "
          "Thm select + Thm reduce intact (structural)", True)

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