class Session < ApplicationRecord
  belongs_to :user
  
  validates :user_id, presence: true
  
  before_create :set_user_agent_and_ip
  
  attr_accessor :current_session_id
  
  def is_current
    id.to_s == Current.session&.id.to_s
  end
  
  def self.cleanup_expired(timeout_minutes = 60)
    where('updated_at < ?', timeout_minutes.minutes.ago).destroy_all
  end
  
  private
  
  def set_user_agent_and_ip
    self.user_agent ||= Current.user_agent if Current.respond_to?(:user_agent)
    self.ip_address ||= Current.ip_address if Current.respond_to?(:ip_address)
  end
end