class ClassLessonPlan < Base::ClassLessonPlan
  PARAMETERS = [
    :name,
    :date,
    :unit_number,
    :status,
    :objectives,
    :resources,
    :procedures,
    :evaluation,
    :notes,
    :homework
  ]

  associations_for legacy: true do |a|
    a.belongs_to :school
    a.belongs_to :classroom, keys: :ClassID
    a.belongs_to :class_subject, foreign_key: :SubjectID, primary_key: :ClassSubjectID,
      optional: true
  end

  after_initialize :set_school

  validates :name, :date, presence: true

  scope :ordered, -> { order(:date) }
  scope :order_student_homework, -> { ordered.order('Classes.Name, ClassSubjects.Name') }
  scope :with_homework, -> { where.not(homework: '') }
  scope :with_student, ->(id) { where(Students: { StudentID: id }) if id }
  scope :by_classroom, ->(id) { where(class_id: id) if id }
  scope :by_parent_viewable, -> { merge(Classroom.by_parent_viewable(true)) }
  scope :by_week, ->(range) { where(date: range) if range }

  private
    def set_school
      return unless school_id.zero?

      self.school = classroom.school
    end
end
