import { ref, readonly } from 'vue'
import type { TimeOfDay, Season, DepthZone } from './types'

// ── Module-level singleton refs ──
const depthZone = ref<DepthZone>('midwater')
const timeOfDay = ref<TimeOfDay>('night')
const season = ref<Season>(getCurrentSeason())
const activeEventModifiers = ref<Map<string, string>>(new Map())

function getCurrentSeason(): Season {
  const month = new Date().getMonth()
  if (month >= 2 && month <= 4) return 'spring'
  if (month >= 5 && month <= 7) return 'summer'
  if (month >= 8 && month <= 10) return 'autumn'
  return 'winter'
}

export function useAquaticState() {
  function setDepthZone(zone: DepthZone) {
    depthZone.value = zone
  }

  function setTimeOfDay(tod: TimeOfDay) {
    timeOfDay.value = tod
  }

  function setSeason(s: Season) {
    season.value = s
  }

  function setEventModifier(eventId: string, cssClass: string) {
    const next = new Map(activeEventModifiers.value)
    next.set(eventId, cssClass)
    activeEventModifiers.value = next
  }

  function clearEventModifier(eventId: string) {
    const next = new Map(activeEventModifiers.value)
    next.delete(eventId)
    activeEventModifiers.value = next
  }

  return {
    depthZone: readonly(depthZone),
    timeOfDay: readonly(timeOfDay),
    season: readonly(season),
    activeEventModifiers: readonly(activeEventModifiers),
    setDepthZone,
    setTimeOfDay,
    setSeason,
    setEventModifier,
    clearEventModifier,
  }
}
