class StudentDetail < ApplicationRecord
  audited associated_with: :student, only: [
    :BirthCity,
    :BirthCountryID,
    :BirthStateID,
    :MultiBirthStatus
  ]

  include Base::StudentDetail

  enum ell_instrument_used: {
    no_value: -1,
    other: 4,
    wida_access: 5,
    w_apt: 6,
    not_available: 7,
    not_assessed_without_cause: 8,
    not_assessed_with_cause: 9,
    wida_alternate: 10,
    annual_las: 11,
    wida_screener: 12
  }, _prefix: true

  belongs_to :student, foreign_key: :StudentID, inverse_of: :detail
  belongs_to :birth_country, class_name: 'IsoCountry', foreign_key: :BirthCountryID,
    inverse_of: :student_details, optional: true
  belongs_to :birth_state, class_name: 'UsState', foreign_key: :BirthStateID,
    inverse_of: :student_details, optional: true

  before_save :clear_init_school_us_entry, if: :united_states?

  validates :birth_city, length: { maximum: 30 }
  validates :init_school_entry_us, presence: true, unless: :united_states?

  private
    def united_states?
      return true unless student.school.ed_fi_system_indiana?

      birth_country_id == 236 || birth_country_id.nil?
    end

    def clear_init_school_us_entry
      self.init_school_entry_us = nil
    end
end
