"""
Test: Hawking temperature from Gibbons-Hawking Euclidean periodicity
====================================================================

Standard derivation (Gibbons-Hawking 1977):
Wick-rotate Schwarzschild t -> i*tau. The Euclidean Schwarzschild metric:

    ds^2 = (1 - Rs/r) d(tau)^2 + (1 - Rs/r)^-1 dr^2 + r^2 dOmega^2

near r = Rs has a conical singularity unless tau is periodic with 
a specific period. Setting r = Rs + rho^2 / (Rs) for small rho, to
leading order near the horizon:

    ds^2 ~ (2 rho / Rs)^2 d(tau/... )^2 + ... 

The condition for no conical defect is that tau has period beta_H:

    beta_H = 4 pi Rs = 8 pi M G / c^3     (SI units)
    beta_H = 4 pi Rs                        (G=c=1)

This beta is identified as inverse temperature, giving:

    T_H = hbar c^3 / (8 pi G M k_B)       (SI)
    T_H = 1 / (4 pi Rs) = 1 / (8 pi M)    (G=c=kB=hbar=1)

Framework reading:
------------------
In the framework, Wick rotation isn't a calculational trick -- it's the
projection between the ambient Euclidean (4,0) description and the
brane-intrinsic Lorentzian (3,1) description. Euclidean time tau IS the
w4 coordinate (or related to it by the embedding structure).

So the Gibbons-Hawking argument isn't "impose periodicity on an analytic
continuation"; it's "the brane's w4 embedding near the horizon must be
smooth (no conical defect) as a geometric requirement."

The periodicity 4*pi*Rs falls out of demanding that the embedded region
around the horizon is a smooth piece of E^4 -- a real geometric condition
on the brane, not a computational device.

If that's right, then T_H is forced by embedding smoothness alone -- no
QFT, no Bogoliubov coefficients, no particle-creation argument. The
framework recovers T_H as a purely geometric consequence.

Test: reproduce the periodicity derivation explicitly, verify the
coefficient, and check what the framework claims about it.
"""

import numpy as np
import sympy as sp

print("="*72)
print("TEST: Hawking temperature from Euclidean periodicity")
print("="*72)
print()

# Symbolic derivation
r, rho, tau, Rs, M = sp.symbols('r rho tau R_s M', positive=True, real=True)

# Near-horizon substitution: r = Rs + rho^2/(Rs) ... let's do it properly.
# We want to expand (1 - Rs/r) near r = Rs.
# Let r = Rs + epsilon. Then 1 - Rs/r = 1 - Rs/(Rs + epsilon) = epsilon/(Rs + epsilon) ~ epsilon/Rs for small epsilon.
# So (1 - Rs/r) ~ epsilon/Rs near horizon.
# And (1 - Rs/r)^-1 ~ Rs/epsilon.

# The dr^2 term: dr = d(epsilon) = d(Rs * sigma^2 / 4) for some substitution. 
# Standard: let rho = 2*sqrt(Rs * epsilon), i.e. epsilon = rho^2/(4*Rs).
# Then dr = rho/(2 Rs) * drho? Let me recompute:
#   epsilon = rho^2 / (4 Rs)
#   d(epsilon) = rho/(2 Rs) d(rho)
#   (d(epsilon))^2 = rho^2 / (4 Rs^2) drho^2
# The metric term: (Rs/epsilon) d(epsilon)^2 = (Rs/(rho^2/(4 Rs))) * rho^2/(4 Rs^2) drho^2
#                = (4 Rs^2/rho^2) * rho^2/(4 Rs^2) drho^2 = drho^2  ✓
# The time term: (epsilon/Rs) dtau^2 = (rho^2/(4 Rs^2)) dtau^2 = rho^2 * (dtau/(2 Rs))^2
# 
# So near-horizon Euclidean metric:
#   ds^2 = drho^2 + rho^2 * (dtau / (2 Rs))^2 + Rs^2 dOmega^2
# 
# This is R^2 times angular sphere, BUT the angular coordinate is tau/(2 Rs).
# For the R^2 piece to be smooth (no conical defect), tau/(2 Rs) must be
# a periodic coordinate with period 2*pi.
# => tau has period 2*pi * 2*Rs = 4*pi*Rs.

print("Near-horizon analysis:")
print("-"*72)
print()
print("Substitution: let epsilon = r - Rs, then rho = 2*sqrt(Rs*epsilon).")
print()
print("Near-horizon Euclidean metric becomes:")
print()
print("  ds^2 = drho^2 + rho^2 * (d(tau/(2*Rs)))^2 + Rs^2 * dOmega^2")
print()
print("The (rho, tau/(2*Rs)) piece is R^2 in polar coordinates.")
print("For smoothness (no conical defect), the angular coordinate tau/(2*Rs)")
print("must have period 2*pi. Therefore tau has period:")
print()
print("  beta_H = 4*pi*Rs")
print()

# Identify beta_H as inverse temperature: T_H = 1/beta_H
# In units where kB = hbar = 1:
#   T_H = 1/(4*pi*Rs) = 1/(8*pi*M*G/c^2) * c/.... 
# Let me restore units carefully.

# SI: Rs = 2GM/c^2. beta_H has units of "Euclidean time" which after
# Wick rotation corresponds to imaginary time, measured in seconds if
# tau has units of seconds. Then beta_H = 4*pi*Rs/c (need to check units)...
#
# Actually, ds^2 has units of length^2. The term (1-Rs/r)*dt^2 must have units
# of length^2 if dt has units of length (geometrised). In SI, t has units
# of seconds and c*dt has units of length. So to make everything consistent
# in SI: c^2 * dt^2 term.
#
# Euclidean: ds^2_E = (1-Rs/r) c^2 d(tau)^2 + ...  with tau in seconds.
# Near horizon, (1-Rs/r) ~ epsilon/Rs with epsilon = r - Rs.
# Substitution: rho = 2*sqrt(Rs*epsilon), drho^2 = (Rs/epsilon) d(epsilon)^2.
# The time term: c^2 * (epsilon/Rs) dtau^2 = (c^2 * rho^2 / (4 Rs^2)) dtau^2
#              = rho^2 * (c * dtau / (2 Rs))^2
# So the angular coordinate is c*tau/(2*Rs), period 2*pi:
#   c*tau_period/(2*Rs) = 2*pi
#   tau_period = 4*pi*Rs/c

# Standard Hawking result: T_H = hbar*c^3 / (8*pi*G*M*kB)
# Check: beta_H = hbar/(kB*T_H)   (hbar converts to "temperature time")
#   = hbar * 8*pi*G*M / (hbar*c^3)
#   = 8*pi*G*M/c^3
# And Rs = 2GM/c^2, so 4*pi*Rs/c = 4*pi*(2GM/c^2)/c = 8*pi*GM/c^3.  ✓

# So our geometric argument gives tau_period = 4*pi*Rs/c = 8*pi*GM/c^3,
# which equals hbar/(kB*T_H), giving:
#   T_H = hbar*c^3/(8*pi*G*M*kB).  Correct Hawking result.

# Numerical check for solar mass BH:
G = 6.674e-11    # m^3 kg^-1 s^-2
c = 2.998e8      # m/s
hbar = 1.055e-34 # J s
kB = 1.381e-23   # J/K
M_sun = 1.989e30 # kg

M_bh = M_sun
Rs_bh = 2*G*M_bh/c**2
T_H_from_geometry = hbar * c**3 / (8 * np.pi * G * M_bh * kB)
T_H_standard = T_H_from_geometry  # Same thing -- this IS the standard formula

print(f"For a solar-mass black hole (M = {M_bh:.3e} kg):")
print(f"  Schwarzschild radius R_s = {Rs_bh:.6e} m = {Rs_bh*1000:.3f} mm")
print(f"  Hawking temperature T_H = {T_H_from_geometry:.6e} K")
print(f"  (equivalently: {T_H_from_geometry*1e9:.3f} nK)")
print()

print("="*72)
print("FRAMEWORK INTERPRETATION")
print("="*72)
print()
print("Standard Gibbons-Hawking argument:")
print("  1. Wick-rotate t -> i*tau.")
print("  2. Require Euclidean metric to be smooth at horizon.")
print("  3. This forces tau to have period 4*pi*Rs.")
print("  4. Interpret beta_H = 4*pi*Rs as inverse temperature.")
print("  5. Recover Hawking's T_H = hbar*c^3/(8*pi*G*M*kB).")
print()
print("The puzzle in the standard account: why does imposing smoothness on")
print("a mathematical analytic continuation give a physical temperature?")
print("Why does the Euclidean trick work at all? This is usually treated")
print("as a remarkable fact to be accepted.")
print()
print("Framework reading:")
print("  - w4 is the ambient Euclidean direction.")
print("  - Wick-rotated 'tau' is (up to sign/orientation) the w4 coordinate")
print("    in the near-horizon patch. The brane's embedding in w4 is smooth")
print("    as a real geometric fact about E^4.")
print("  - Near a black hole horizon, the embedding has a specific near-conical")
print("    structure. For the embedded region to be smooth (i.e. actually be")
print("    a submanifold of E^4 with no pathology), the w4 coordinate must be")
print("    periodic with period 4*pi*Rs.")
print("  - This periodicity is a GEOMETRIC requirement on the real embedding,")
print("    not a formal requirement on an analytic continuation.")
print("  - The thermal character of Hawking radiation is then a consequence of")
print("    this geometric periodicity: fields on the brane, when they probe the")
print("    near-horizon region, encounter a periodic ambient coordinate, and a")
print("    periodic Euclidean time is the defining feature of a thermal state.")
print()
print("Key move: in the framework, the 'why does it work' puzzle dissolves.")
print("Wick rotation is the projection, not a trick. Periodicity is real, not")
print("formal. The Hawking temperature is the period of real w4 near a horizon.")
print()
print("VERDICT: T_H = hbar*c^3/(8*pi*G*M*kB) is recovered, and the framework")
print("provides an ontological account of *why* the Euclidean argument works.")
print()
print("Caveat: the argument still uses Schwarzschild as a GR solution. The")
print("framework reads the metric; it does not derive Rs. So this is recovery-")
print("with-reframing of the mechanism, not independent derivation of the number.")
print("But the *mechanism* (geometric smoothness of real embedding) is novel")
print("and has explanatory content the standard picture lacks.")
