module SpiritsHelper
  require 'lunartic'
  require 'net/http'
  require 'json'

  CHALDEAN_ORDER = %w[Saturn Jupiter Mars Sun Venus Mercury Moon].freeze

  API_ENDPOINT = "http://www.planetaryhoursapi.com/api/"

  def planetary_hour_and_day_match?(datetime)
    longitude= session[:longitude] ? session[:longitude].to_f : -0.001545
    latitude = session[:latitude] ? session[:latitude].to_f : 51.477928

    data = fetch_planetary_data(datetime, latitude, longitude)
    general_info = data["Response"]["General"]
    planet_for_day = general_info["PlanetaryRuler"]
  
    solar_hours = data["Response"]["SolarHours"]
    lunar_hours = data["Response"]["LunarHours"]
  
    all_hours = solar_hours.merge(lunar_hours)
  
    planet_for_hour = all_hours.each_value.find do |hour|
      start_time = DateTime.parse(hour["Start"])
      end_time = DateTime.parse(hour["End"])
  
      datetime.between?(start_time, end_time)
    end&.fetch("Ruler", nil)
  
    planet_for_day == planet_for_hour && planet_for_hour
  end
  
  def fetch_planetary_data(datetime, latitude, longitude)
    date = datetime.strftime('%Y-%m-%d')
    uri = URI("#{API_ENDPOINT}#{date}/#{latitude},#{longitude}")
    response = Net::HTTP.get(uri)
    JSON.parse(response)
  end
  
def lunar_date(date)
  moon = Lunartic.on_date(date)
  age = moon.day.floor

  if [2, 4, 6, 8, 10, 12, 14].include?(age)
    return "#{moon_phase_emoji(date)} The moon is #{age} days old and is suitable for workings. ✅"
  else
    return "#{moon_phase_emoji(date)} The moon is #{age} days old, which is not recommended for workings. ⛔️"
  end 
end

def moon_phase_emoji(date)
  moon = Lunartic.on_date(date)
  phase = moon.phase
  
  case phase
  when :new
    '🌑' # New Moon
  when :waxing_crescent
    '🌒' # Waxing Crescent Moon
  when :first_quarter
    '🌓' # First Quarter Moon
  when :waxing_gibbous
    '🌔' # Waxing Gibbous Moon
  when :full
    '🌕' # Full Moon
  when :waning_gibbous
    '🌖' # Waning Gibbous Moon
  when :last_quarter
    '🌗' # Last Quarter Moon
  when :waning_crescent
    '🌘' # Waning Crescent Moon
  else
    '❓' # Unknown phase
  end
end

def time_matches_rank?(datetime, rank)
  latitude = session[:latitude].to_f
  
  sunrise_time = session[:sunrise] ? DateTime.parse(session[:sunrise]) : datetime.change(hour: 5)
  sunset_time = session[:sunset] ? DateTime.parse(session[:sunset]) : datetime.change(hour: 20)

  # Adjust twilight duration based on latitude. This is a rough approximation.
  twilight_adjustment = [30 + (latitude.abs / 90) * 30, 60].min.minutes

  dawn_time = sunrise_time - twilight_adjustment if sunrise_time
  twilight_end_time = sunset_time + twilight_adjustment if sunset_time

  case rank.time
  when 'anytime'
    true
  when 'dawn to sunrise'
    return false unless sunrise_time
    datetime.between?(dawn_time, sunrise_time)
  when 'sunrise to noon'
    return false unless sunrise_time
    datetime.between?(sunrise_time, datetime.change(hour: 12))
  when '3pm to sunrise'
    (datetime.hour >= 15) || (datetime <= sunrise_time)
  when 'anytime excluding twilight'
    return false unless twilight_end_time
    !datetime.between?(sunset_time, twilight_end_time)
  when 'anytime in a desolate place'
    true
  when 'on great occasions'
    datetime.hour.between?(10, 14)
  else
    false
  end
end  

  def planet_for_day(weekday)
    case weekday
    when 'Sunday'
      'Sun'
    when 'Monday'
      'Moon'
    when 'Tuesday'
      'Mars'
    when 'Wednesday'
      'Mercury'
    when 'Thursday'
      'Jupiter'
    when 'Friday'
      'Venus'
    when 'Saturday'
      'Saturn'
    end
  end
end