"""
Scope-boundary test 3: de Sitter cosmological horizon temperature

Claim: A static observer in a de Sitter spacetime with Hubble constant H
sees a cosmological horizon at r_dS = 1/H (in geometrised units), and
the vacuum appears thermal at the Gibbons-Hawking temperature

    T_dS = hbar * H / (2 pi c k_B) = kappa / (2 pi)

with kappa = H. This completes the triple of thermal-horizon results:
  - Black hole event horizon (Hawking T)      -- sanitation v2 check 14
  - Rindler acceleration horizon (Unruh T)    -- scope test 2
  - Cosmological horizon (de Sitter T)        -- this test

All three have the same structural origin: surface gravity kappa gives
T = kappa / (2 pi) via Euclidean-time regularity at the horizon.

Setup (geometrised units G = c = hbar = k_B = 1):

de Sitter space in static coordinates:

    ds^2 = -(1 - H^2 r^2) dt^2 + (1 - H^2 r^2)^(-1) dr^2 + r^2 dOmega^2

Horizon at r_dS = 1/H where the (tt) and (rr) components degenerate.
The framework's u-reading:

    u(r) = u_0 * sqrt(1 - H^2 r^2)

Framework prediction: same structural mechanism as Schwarzschild and
Rindler. Euclidean time near the horizon is periodic with period
2 pi / kappa, giving T_dS = H / (2 pi).

Additional interest: de Sitter appears in the framework's own account
of Lambda. Paper 1's story is that vacuum energy produces uniform
baseline w4 advance u_0, which at large scales manifests as the
de Sitter exponential expansion. So this test checks that the
framework's OWN cosmological solution carries the expected horizon
temperature.
"""

import numpy as np
import sympy as sp

print("=" * 72)
print("SCOPE-BOUNDARY TEST 3: DE SITTER HORIZON TEMPERATURE")
print("=" * 72)
print()

# ------------------------------------------------------------------
# Symbolic setup
# ------------------------------------------------------------------
r, t, H, u0, tau_E = sp.symbols('r t H u_0 tau_E', real=True, positive=True)

f_dS = 1 - H**2 * r**2
u_dS = u0 * sp.sqrt(f_dS)

print("Static dS line element:")
print("  ds^2 = -(1 - H^2 r^2) dt^2 + (1 - H^2 r^2)^(-1) dr^2 + r^2 dOmega^2")
print()
print(f"-g_tt = f(r) = 1 - H^2 r^2")
print(f"u(r)        = {u_dS}")
print()

# ------------------------------------------------------------------
# Check 1: Horizon location
# ------------------------------------------------------------------
print("-" * 72)
print("Check 1: Cosmological horizon from u = 0")
print("-" * 72)
horizons = sp.solve(f_dS, r)
print(f"Solving f(r) = 0:  r = {horizons}")
print(f"Positive root     :  r_dS = 1/H")
print(f"Standard dS result:  r_dS = 1/H  (Hubble radius)")
print("=> Match.")
print()

# ------------------------------------------------------------------
# Check 2: Surface gravity
# ------------------------------------------------------------------
print("-" * 72)
print("Check 2: Surface gravity at r_dS = 1/H")
print("-" * 72)
df_dr = sp.diff(f_dS, r)
print(f"df/dr = {df_dr}")
# kappa = (1/2) |df/dr| at the horizon, evaluated with Killing vector
# normalised to have |K| = 1 at r = 0 (the observer's worldline)
kappa_dS = sp.Abs(df_dr.subs(r, 1/H)) / 2
kappa_dS = sp.simplify(kappa_dS)
print(f"kappa = (1/2) |df/dr|_{{r=1/H}} = {kappa_dS}")
print(f"Standard dS result: kappa = H")
print("=> Match.")
print()

print("Note on Killing vector normalisation:")
print("  At r = 0 (observer's worldline), g_tt = -1, so the timelike")
print("  Killing vector K = d/dt has |K| = 1 there. t is the static")
print("  observer's proper time. With this normalisation, kappa = H.")
print()

# ------------------------------------------------------------------
# Check 3: Euclidean periodicity
# ------------------------------------------------------------------
print("-" * 72)
print("Check 3: Euclidean periodicity near the horizon")
print("-" * 72)
print()
print("Expand f(r) near r_dS = 1/H: let rho = 1/H - r (small), then")
print("  f(r) = 1 - H^2 (1/H - rho)^2")
print("       = 1 - (1 - H rho)^2")
print("       = 2 H rho - H^2 rho^2")
print("       ~ 2 H rho                    (leading order, small rho)")
print()
print("Define a new radial coordinate chi via dchi = drho / sqrt(f):")
print("  drho/dchi = sqrt(2 H rho)")
print("  rho = (1/2) (H/2) chi^2 * something ... let's do this cleanly.")
print()

# Change of variable: let chi^2 = (2/H) * rho, so chi = sqrt(2 rho/H),
# rho = H chi^2 / 2.
# Then f = 2 H rho = H^2 chi^2.
# sqrt(f) = H chi.
# dr = -drho = -(H chi) dchi, so (dr)^2 / f = (H^2 chi^2 dchi^2) / (H^2 chi^2) = dchi^2. Good.
# Euclidean line element near horizon:
# ds_E^2 = f dtau_E^2 + dr^2 / f + transverse
#        = H^2 chi^2 dtau_E^2 + dchi^2 + transverse
# This is flat 2D polar coordinates if H tau_E has period 2 pi, i.e.,
# tau_E has period 2 pi / H.
print("Introduce chi via rho = H chi^2 / 2. Then:")
print("  f      = H^2 chi^2")
print("  dr^2/f = dchi^2")
print("  Euclidean (tau_E, r) part = H^2 chi^2 dtau_E^2 + dchi^2")
print()
print("This is flat polar coordinates (chi = radius, H*tau_E = angle).")
print("Regularity at chi = 0 (the horizon) requires H*tau_E to have")
print("period 2 pi, so tau_E period = 2 pi / H.")
print()

tau_E_period = 2 * sp.pi / H
T_dS = 1 / tau_E_period
print(f"Euclidean time period: {tau_E_period}")
print(f"Temperature T_dS = 1 / period = {T_dS}")
print()

print(f"Framework prediction: T_dS = H / (2 pi)")
print(f"Standard Gibbons-Hawking result: T_dS = H / (2 pi)")
print("=> Match.")
print()

# ------------------------------------------------------------------
# Check 4: Dimensional and numerical values
# ------------------------------------------------------------------
print("-" * 72)
print("Check 4: Numerical value for the observed universe")
print("-" * 72)
print()
print("Restoring constants:  T_dS = hbar * H / (2 pi c k_B)")
print()

hbar = 1.054_571_817e-34
c_light = 2.997_924_58e8
k_B = 1.380_649e-23

# H_0 in SI
# H_0 = 70 km/s/Mpc = 70 / (3.0857e19 km) = 2.27e-18 /s  (approximately)
# Use the observed H_0 ~ 67 km/s/Mpc for Planck-like values
H0_kmsMpc = 67.4
Mpc_in_km = 3.0857e19
H0_SI = H0_kmsMpc / Mpc_in_km   # 1/s

T_dS_val = hbar * H0_SI / (2 * np.pi * k_B)
# Note: no factor of c here if we use H in 1/s; the factor c is absorbed
# because in geometrised units H has dimensions of 1/length, and we
# divide by c to make 1/time. Let's be careful:
# T = hbar * kappa / (2 pi c k_B) when kappa is a gravitational
# surface gravity with units m/s^2. For de Sitter in SI, the "surface
# gravity" is c * H (to get m/s^2 from H in 1/s).
# So properly:
kappa_dS_SI = c_light * H0_SI  # m/s^2
T_dS_SI = hbar * kappa_dS_SI / (2 * np.pi * c_light * k_B)
# = hbar * H0 / (2 pi k_B)
print(f"H_0 (observed) ~ {H0_kmsMpc} km/s/Mpc = {H0_SI:.3e} /s")
print(f"kappa_dS = c * H_0 = {kappa_dS_SI:.3e} m/s^2")
print(f"T_dS     = hbar * H_0 / (2 pi k_B) = {T_dS_SI:.3e} K")
print()
print(f"Reference: Gibbons-Hawking T for observed Lambda ~ 10^-30 K")
print("(specifically ~2.7e-30 K for H_0 ~ 67 km/s/Mpc).")
print()

# Check we get the right order
print(f"Our calculation: {T_dS_SI:.3e} K  -- matches order of magnitude.")
print()

# ------------------------------------------------------------------
# Check 5: All three thermal horizons side by side
# ------------------------------------------------------------------
print("-" * 72)
print("Check 5: The thermal-horizon triple")
print("-" * 72)
print()
print("The framework recovers T = kappa / (2 pi) at all three horizon types:")
print()
print("  Horizon type           | kappa       | Temperature")
print("  -----------------------|-------------|------------------------")
print("  Schwarzschild (mass M) | 1/(4M)      | T_H = 1/(8 pi M)")
print("  Rindler (accel alpha)  | alpha       | T_U = alpha/(2 pi)")
print("  de Sitter (Hubble H)   | H           | T_dS = H/(2 pi)")
print()
print("All three come from:")
print("  (1) u = u_0 sqrt(-g_tt) vanishes at the horizon")
print("  (2) Wick rotation near the horizon reveals flat 2D polar geometry")
print("  (3) Regularity at the horizon tip fixes Euclidean-time period")
print("  (4) Period = 2 pi / kappa gives T = kappa / (2 pi)")
print()
print("Framework-native interpretation: the Euclidean periodicity is a")
print("REAL periodicity in the ambient w4 direction, and brane fields")
print("probing the horizon encounter this periodicity and register as")
print("thermal. Not a calculational trick -- a geometric fact about the")
print("embedding's structure near horizon surfaces.")
print()

# ------------------------------------------------------------------
# Check 6: Framework-specific interest for cosmology
# ------------------------------------------------------------------
print("-" * 72)
print("Check 6: Framework interpretation of the cosmological horizon T")
print("-" * 72)
print("""
In the framework's own cosmological story:
  - Vacuum has baseline mass-energy density rho_vac (standard QFT).
  - This couples to w4 via the framework's postulate, producing uniform
    baseline w4 advance rate u_0.
  - u_0 is identified with what LCDM calls Lambda (sanitation v2 check 10).
  - At large scales, a universe with uniform vacuum coupling behaves as
    de Sitter, with H^2 ~ Lambda / 3.
  - Therefore: the framework's own cosmological attractor is de Sitter,
    and T_dS is the temperature observed at its horizon.

Scope reading:
  - The existence and magnitude of Lambda -- NOT predicted (still QFT's
    vacuum energy problem, 120 orders off in standard approach).
  - The de Sitter form (exponential expansion) -- ONTOLOGICALLY homed
    by the uniform u_0 picture.
  - T_dS = H / (2 pi) -- IN SCOPE, recovered by the same thermal-horizon
    mechanism as Hawking and Unruh.

The framework is not claiming to derive the observed T_dS ~ 10^-30 K
from first principles; it inherits H from the observed Lambda. What
it does is provide the thermal-horizon MECHANISM consistently across
all three horizon types, via a single geometric argument (Euclidean
regularity in the ambient w4 direction).
""")

print("=" * 72)
print("TEST 3 RESULT: PASS")
print("=" * 72)
print("Framework recovers T_dS = H / (2 pi) by the same mechanism used")
print("for Hawking T and Unruh T. Third consistent application confirms")
print("that the thermal-horizon argument is universal: it applies to any")
print("Killing horizon, not just black hole event horizons.")
