module CalendarHelper
  def planet_for_day(weekday)
    {
      "Sunday" => "Sun",
      "Monday" => "Moon",
      "Tuesday" => "Mars",
      "Wednesday" => "Mercury",
      "Thursday" => "Jupiter",
      "Friday" => "Venus",
      "Saturday" => "Saturn"
    }[weekday]
  end

  def moon_phase_matches?(date)
    # Implement your moon phase calculation logic here
    # Return true if the current moon phase matches the required phase for spirits
    # You might want to use a gem like ruby-moon for accurate calculations
    true # Placeholder return value
  end

  def celestial_emoji(planet)
    {
      "Sun" => "☉",
      "Moon" => "☽",
      "Mars" => "♂",
      "Mercury" => "☿",
      "Jupiter" => "♃",
      "Venus" => "♀",
      "Saturn" => "♄"
    }[planet]
  end

  def current_time_valid?(date)
    rank = Rank.find_by(planet: planet_for_day(date.strftime("%A")))
    return false unless rank

    case rank.time.downcase
    when "anytime"
      true
    when "dawn to sunrise"
      current_hour = Time.now.hour
      (4..6).include?(current_hour)
    when "sunrise to noon"
      current_hour = Time.now.hour
      (6..12).include?(current_hour)
    when "3pm to sunrise"
      current_hour = Time.now.hour
      (15..23).include?(current_hour) || (0..6).include?(current_hour)
    when "anytime excluding twilight"
      current_hour = Time.now.hour
      !(4..6).include?(current_hour) && !(17..19).include?(current_hour)
    when "anytime in a desolate place"
      true # This would need additional context to determine
    when "on great occasions"
      # You might want to add logic here to determine what constitutes a "great occasion"
      true
    else
      true # Default to allowing if we don't recognize the time restriction
    end
  end

  def time_requirement_icon(time)
    icon = case time.downcase
    when /sunrise/
      "🌅"
    when /dawn/
      "🌄"
    when /noon/
      "☀️"
    when /twilight/
      "🌆"
    when /anytime/
      "⌛"
    when /desolate/
      "🏚️"
    when /great occasions/
      "👑"
    else
      "⏰"
    end
    
    content_tag :span, icon, class: "time-icon", title: time
  end

  def sunrise_time(date)
    calc = SunCalc.new(date, latitude, longitude)
    sunrise = calc.sunrise
    format_time_with_date(sunrise, date)
  end

  def sunset_time(date)
    calc = SunCalc.new(date, latitude, longitude)
    sunset = calc.sunset
    format_time_with_date(sunset, date)
  end

  private

  def format_time_with_date(time, date)
    "#{time.strftime('%H:%M')} #{date.strftime('%d %b')}"
  end

  def latitude
    # Replace with your location's latitude
    52.5200 # Example: Berlin
  end

  def longitude
    # Replace with your location's longitude
    13.4050 # Example: Berlin
  end
end 