module Base::Discipline::StudentDetention
  extend ActiveSupport::Concern
  include Castable

  included do
    self.table_name = :DisciplineDetentions
    self.primary_key = :DDID

    cast_as_boolean :Waived, true

    alias_attribute :id, :DDID
    alias_attribute :school_id, :SchoolID
    alias_attribute :student_id, :StudentID
    alias_attribute :posted_on, :DatePosted
    alias_attribute :served_on, :DateServed
    alias_attribute :length, :Length
    alias_attribute :completed, :Completed
    alias_attribute :comments, :Comments
    alias_attribute :author_id, :AuthorID
    alias_attribute :monitor_id, :MonitorID
    alias_attribute :manager_notified, :ManagerNotified
    alias_attribute :family_notified, :FamilyNotified
    alias_attribute :student_notified, :StudentNotified
    alias_attribute :waived, :Waived
    alias_attribute :days, :Days

    before_validation :set_defaults, if: :new_record?

    private
      def set_defaults
        self.school = student.school if school.blank?
        self.completed = false unless completed?
        self.comments = '' unless comments?
        self.days = 0 unless days?
        self.posted_on = Time.zone.now unless posted_on?
        self.served_on = Time.zone.now unless served_on?
        self.length = 0 unless length?
      end
  end
end
