class LunchCycle < ApplicationRecord
  include Base::LunchCycle

  belongs_to :school, foreign_key: 'SchoolID', inverse_of: :lunch_cycles

  has_many :student_lunch_orders, foreign_key: 'LunchCycleID', inverse_of: :lunch_cycle,
    dependent: :destroy
  has_many :student_item_orders, foreign_key: 'LunchCycleID', inverse_of: :lunch_cycle,
    dependent: :destroy

  scope :by_open, ->(flag) { where(open: flag) unless flag.nil? }
  scope :by_ala_carte, ->(flag) { where(ala_carte: flag) unless flag.nil? }
  scope :by_open_or_ala_carte, ->(flag) {
    where(open: flag).or(where(ala_carte: flag)) unless flag.nil?
  }
  scope :by_active, ->(date) { where('EndDate >= ?', date) if date }

  scope :by_start_and_end_dates, -> do
    where.not(start_date: [nil, 0o000 - 0o0 - 0o0], end_date: [nil, 0o000 - 0o0 - 0o0])
  end
end
