import { useEffect, useState } from 'react'
import { isBedtime } from '../lib/night'

// Tracks whether it's late enough to shift Petal into its calm night mode (dark
// theme + falling stars instead of petals). Re-checks once a minute so the swap
// happens on its own as the clock rolls past 11pm — no reload needed. Returns
// the boolean and also toggles a `petal-night` class on <html> so the CSS token
// overrides cascade to everything (body background, panels, portals included).
export function useNightMode(): boolean {
  const [night, setNight] = useState(() => isBedtime())

  useEffect(() => {
    const tick = () => setNight(isBedtime())
    tick()
    const id = setInterval(tick, 60_000)
    return () => clearInterval(id)
  }, [])

  useEffect(() => {
    document.documentElement.classList.toggle('petal-night', night)
    return () => document.documentElement.classList.remove('petal-night')
  }, [night])

  return night
}
