class Setting < ApplicationRecord
  validates :key, presence: true, uniqueness: true

  # Get a setting value by key, with optional fallback
  def self.get(key, default = nil)
    find_by(key: key)&.value || default
  end

  # Set a setting value by key
  def self.set(key, value)
    find_or_initialize_by(key: key).update(value: value)
  end

  # Convenience method for ipapi.is API key
  def self.ipapi_key
    get('ipapi_key', ENV['IPAPI_KEY'])
  end

  # Convenience method for event retention days (default: 90 days)
  def self.event_retention_days
    get('event_retention_days', '90').to_i
  end
end
