class QuizResult < ApplicationRecord
  validates :share_id, presence: true, uniqueness: true
  validates :player_name, :stats, :score, presence: true

  before_validation :generate_share_id, on: :create

private

  def generate_share_id
    self.share_id = SecureRandom.urlsafe_base64(8) if share_id.blank?
    # Ensure uniqueness (very unlikely collision but good practice)
    generate_share_id if QuizResult.exists?(share_id: share_id)
  end

end
