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

  def planetary_hour_and_day_match?(datetime, planet)
    planet_for_day(datetime.strftime('%A')) == planet && planetary_hour(datetime) == planet
  end

  def planetary_hour(datetime)
    day_start = datetime.beginning_of_day
    hour_duration = (datetime.end_of_day - day_start) / 12.0
    hour_number = ((datetime - day_start) / hour_duration).floor

    weekday = datetime.strftime('%A')
    start_index = CHALDEAN_ORDER.index(planet_for_day(weekday))
    planet_index = (start_index + hour_number) % 7

    CHALDEAN_ORDER[planet_index]
  end

  def time_matches_rank?(datetime, rank)
    case rank.time
    when 'anytime'
      true
    when 'dawn to sunrise'
      # Implement logic for dawn to sunrise
      # This will require additional logic to calculate dawn based on location and date
      # Placeholder for this example
      datetime.hour.between?(5, 7)
    when 'sunrise to noon'
      datetime.hour.between?(6, 12)
    when '3pm to sunrise'
      datetime.hour.between?(15, 24) || datetime.hour.between?(0, 6)
    when 'anytime excluding twilight'
      # Placeholder logic for excluding twilight
      !datetime.hour.between?(18, 20)
    when 'anytime in a desolate place'
      true
    when 'on great occasions'
      # Placeholder - you'd have to define what "on great occasions" means in terms of time
      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