#!/usr/bin/env python3
"""AP46 -- "The Stretch" -- independent verification script.

Reproduces, from CODATA 2022 alone, every number behind the age-cycle row on
lucid.rodeo/prereg/ap46/:

  (21/18) * alpha^{-18} * tau_C  =  13.830 Gyr

No dependencies beyond the Python 3 standard library. No network access.

    python3 verify_ap46.py

Source: AP46_The_Stretch_FINAL_v1_5.md section 9, anchored on
github.com/ajgreyling/the420code-proof (tag ap45-ap46-locked-2026-09-02).
"""
from __future__ import annotations

import math

# --------------------------------------------------------------------------- #
# CODATA 2022 -- the only measured inputs (named on AP46 section 9)
# --------------------------------------------------------------------------- #
ALPHA = 7.2973525643e-3     # fine-structure constant
M_E = 9.1093837139e-31      # electron mass, kg
HBAR = 1.054571817e-34      # reduced Planck constant, J*s
C = 299792458.0             # speed of light, m/s (exact)
SEC_PER_GYR = 365.25 * 24 * 3600 * 1e9
# 1 Gyr^{-1} in km s^{-1} Mpc^{-1}
KM_S_MPC_PER_GYR = 977.792221680665

AGE_PLANCK_BAO = 13.787     # Planck 2018 Table 2, TT,TE,EE+lowE+lensing+BAO
AGE_PLANCK_ALONE = 13.797   # same table, Planck-alone


def tau_c(hbar: float = HBAR, m_e: float = M_E, c: float = C) -> float:
    """Reduced Compton time ħ/(m_e c^{2}), seconds."""
    return hbar / (m_e * c * c)


def cycle_gyr(alpha: float = ALPHA) -> float:
    return (21 / 18) * (alpha ** -18) * tau_c() / SEC_PER_GYR


def main() -> int:
    t_c = tau_c()
    bleed_s = (ALPHA ** -18) * t_c
    bleed_gyr = bleed_s / SEC_PER_GYR
    cyc = cycle_gyr()
    lane = cyc / 21
    h0_refused = KM_S_MPC_PER_GYR / cyc
    unreduced = cyc * 2 * math.pi
    vs_bao = (cyc - AGE_PLANCK_BAO) / AGE_PLANCK_BAO * 100
    vs_alone = (cyc - AGE_PLANCK_ALONE) / AGE_PLANCK_ALONE * 100
    from_centre = AGE_PLANCK_BAO - cyc  # paper: "sits 0.04 inside" the cycle

    bar = "=" * 76
    print(bar)
    print("AP46 -- The Stretch -- independent verification (CODATA 2022, stdlib only)")
    print(bar)

    print("\nTick -- reduced Compton time (the named ruling; the unreduced is 2pi longer):")
    print(f"  tau_C = hbar / (m_e c^2)               = {t_c:.10e} s")
    print("  paper                                = 1.2880886656e-21 s")

    print("\nBleed pass -- eighteen doors, gates closed:")
    print(f"  alpha^{{-18}} * tau_C                    = {bleed_s:.4e} s = {bleed_gyr:.3f} Gyr")
    print("  paper                                = 3.7408e17 s / 11.854 Gyr")

    print("\nKS-STRETCH.3 -- the age chain. LOCKED 2026-09-02.")
    print(f"  (21/18) * alpha^{{-18}} * tau_C          = {cyc:.3f} Gyr")
    print(f"  vs Planck 2018 (lensing+BAO) 13.787   = {vs_bao:+.2f}%")
    print(f"  vs Planck-alone 13.797                = {vs_alone:+.2f}%")
    print(f"  measured sits {abs(from_centre):.3f} Gyr from the cycle (paper: 0.04 inside)")
    print(f"  lane-time = cycle/21                  = {lane:.3f} Gyr")
    print(f"  window (one lane either side)         = {cyc - lane:.2f} to {cyc + lane:.2f} Gyr")
    print("  paper window                         = 13.17 to 14.49 Gyr")
    print(f"  unreduced-tick lever                  = {unreduced:.1f} Gyr (paper 86.9)")

    print("\nH0 -- REFUSED. The chain yields a time, not today's rate:")
    print(f"  977.8 / cycle                         = {h0_refused:.1f} km s^{{-1}} Mpc^{{-1}}")
    print("  paper                                = ~70.7   -- not claimed; KS-45.1 owns H0")

    print(f"\n{bar}")
    print("KS-STRETCH.3: cycle lands at 13.830 Gyr, inside the one-lane window.")
    print("This script does not claim H0 and does not close D-STRETCH.1 (AP48).")
    print(bar)

    lands = abs(cyc - 13.830) < 0.001
    window_ok = 13.16 < (cyc - lane) < 13.18 and 14.48 < (cyc + lane) < 14.50
    refused_ok = abs(h0_refused - 70.7) < 0.05
    return 0 if (lands and window_ok and refused_ok) else 1


if __name__ == "__main__":
    raise SystemExit(main())
