class Nursing::Log < ApplicationRecord
  include Base::Nursing::Log

  audited

  associations_for legacy: true do |a|
    a.belongs_to :student
    a.belongs_to :author, class_name: 'User', primary_key: :UserID,
      inverse_of: :nursing_authored_logs
  end

  belongs_to :complaint, foreign_key: :SMCID, inverse_of: :logs, optional: true

  delegate :school, to: :student
  delegate :family, to: :student
  delegate :user, to: :family, prefix: :family

  after_create :notify_family, if: :alert?

  scope :ordered, -> { order(entry_date: :desc) }
  scope :by_students, ->(ids) { where(student_id: ids) if ids.present? }
  scope :with_date_range, ->(range) { where(entry_date: range) if range.present? }

  def convert_datetime
    return if entry_date.present?

    self.entry_date = datetime + 6.hours
    save(validate: false)
  end

  private
    def notify_family
      Notification::Nursing::PanJob.perform_async(id)
    end
end
