from saltshaker import get_visibility_windows, get_salt_observer
from astropy.coordinates import SkyCoord
from astropy.time import Time
import astropy.units as u
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

observer = get_salt_observer()
target = SkyCoord.from_name('Sirius')
date = '2026-01-15'

# 1. Calculate the estimated tracks and twilight
windows = get_visibility_windows(target, date)
start_time = Time(f"{date} 12:00:00")
eve_twi = observer.twilight_evening_astronomical(start_time, which='next')
morn_twi = observer.twilight_morning_astronomical(eve_twi, which='next')

# 2. Create the visualization
fig, ax = plt.subplots(figsize=(12, 4))

# Shade the dark time
ax.axvspan(eve_twi.plot_date, morn_twi.plot_date, color='black', alpha=0.15, label='Astronomical Dark')

# Shade the estimated visibility windows
for i, w in enumerate(windows):
    ax.axvspan(w.start_time.plot_date, w.end_time.plot_date, color='green', alpha=0.6,
               label='Est. SALT Visibility' if i==0 else "")
    # Label the tracks
    mid_time = w.start_time.plot_date + (w.end_time.plot_date - w.start_time.plot_date)/2
    ax.text(mid_time, 0.5, f"Track {i+1}", ha='center', va='center', fontweight='bold')

plt.title(f"Preliminary Nightly Windows: {target.name} on {date}")
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.set_yticks([])
plt.xlabel("Time (UTC)")
plt.legend(loc='upper right')
plt.grid(True, axis='x', alpha=0.3)
plt.show()