class School::Legacy::LunchesController < ApplicationController
  include DateRangeHelper

  def index
    if params[:dates].present?
      lunches = lunch_days.where(date: date_range(params[:dates])).order(:date)
      data = lunches.map { |l| lunch_props(l) }
    else
      lunch = lunch_days.find_by(date: Time.zone.today)
      meals = lunch ? lunch.meals : []
      data = meals.map { |m| meal_props(m) }
    end
    render_success :ok, json: data
  end

  def future_lunch
    flag = lunch_days.where('LunchDate >= ?', Time.zone.today).limit(1).present?
    render_success :ok, json: { future_lunch: flag }
  end

  private
    def lunch_days
      current_school.current_year.lunch_days
    end

    def lunch_props(lunch)
      {
        id: lunch.id,
        date: lunch.date,
        meals: lunch.meals.map { |m| meal_props(m) }
      }
    end

    def meal_props(meal)
      {
        id: meal.id,
        meal: meal.name,
        side: meal.description
      }
    end
end
