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 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