class User < ActiveRecord::Base
  enum role: [:user, :vip, :admin]
  after_initialize :set_default_role, if: :new_record?
  validates :email, presence: true, uniqueness: true
  validates :soft_token, presence: true, uniqueness: true

  def set_default_role
    self.role ||= :user
  end

  def soft_user?
    self.email.empty?
  end

  def signed_in?
    !soft_user?
  end

  def display_name
    self.email || "Guest#{self.soft_token[0..5]}"
  end

  def sit_in(seat)
    seat.sit self
  end

  def stand_from(table)
    return if table == nil
    @seats = []
    if self.id
      @seats << table.seats.where(user_id: self.id)
    end
    if self.soft_token
      @seats << table.seats.where(user_soft_token: self.soft_token)
    end
    @seats.flatten.each do |seat|
      seat.stand(self)
    end
  end

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable
end
