# typed: strict
class Domain::UserJobEvent::AddTrackedObject < Domain::UserJobEvent
  self.table_name = "domain_user_job_event_add_tracked_objects"
  belongs_to :log_entry, class_name: "HttpLogEntry"
  validates :log_entry_id, uniqueness: { scope: :kind }

  attr_json :requested_at, ActiveModelUtcTimeValue.new

  enum :kind, %i[favs followed_by following gallery]

  validates :kind, presence: true, inclusion: { in: self.kinds.keys }
  validates :num_added, presence: true, numericality: { only_integer: true }
  validates :num_total, presence: true, numericality: { only_integer: true }
  validates :requested_at, presence: true
  validate :num_added_must_be_less_than_or_equal_to_num_total

  before_validation :set_requested_at

  private

  sig { void }
  def num_added_must_be_less_than_or_equal_to_num_total
    if (na = self.num_added) && (nt = self.num_total) && na > nt
      if na > nt
        self.errors.add(:num_added, "must be less than or equal to num_total")
      end
    end
  end

  sig { void }
  def set_requested_at
    if le = self.log_entry
      self.requested_at = le.requested_at&.utc
    end
  end
end
