class RequestHost < ApplicationRecord
  validates :hostname, presence: true, uniqueness: true
  validates :usage_count, presence: true, numericality: { greater_than: 0 }

  has_many :events, dependent: :nullify

  # Class method to find or create a host
  def self.find_or_create_host(hostname)
    find_or_create_by(hostname: hostname) do |host|
      host.usage_count = 1
      host.first_seen_at = Time.current
    end
  end

  # Increment usage count
  def increment_usage!
    increment!(:usage_count)
  end
end
