class Invitation < ApplicationRecord
  # Relations
  belongs_to :user
  belongs_to :invitee, class_name: "User", foreign_key: 'invited_user_id', optional: true

  # Validations
  validates_presence_of :user

  # Hooks
  before_create :generate_token

  # Scopes
  scope :unused, -> { where(used_at: nil) }
  scope :used, -> { where.not(used_at: nil) }

  private

  def generate_token
    self.token = SecureRandom.hex(8)
  end
end
