# frozen_string_literal: true

require 'fileutils'

# MaxMind GeoIP Configuration
Rails.application.configure do
  config.maxmind = ActiveSupport::OrderedOptions.new

  # Database configuration
  config.maxmind.database_url = ENV.fetch('MAXMIND_DATABASE_URL', 'https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb')
  config.maxmind.database_type = 'GeoLite2-Country'

  # Local storage paths
  config.maxmind.storage_path = Rails.root.join('db', 'geoip')
  config.maxmind.database_filename = 'GeoLite2-Country.mmdb'

  # Update configuration
  config.maxmind.auto_update_enabled = ENV.fetch('MAXMIND_AUTO_UPDATE', 'true').downcase == 'true'
  config.maxmind.update_interval_days = ENV.fetch('MAXMIND_UPDATE_INTERVAL_DAYS', 7).to_i
  config.maxmind.max_age_days = ENV.fetch('MAXMIND_MAX_AGE_DAYS', 30).to_i

  # Note: MaxMind DB has its own internal caching, no additional caching needed

  # Fallback settings
  config.maxmind.fallback_country = ENV.fetch('MAXMIND_FALLBACK_COUNTRY', nil)
  config.maxmind.enable_fallback = ENV.fetch('MAXMIND_ENABLE_FALLBACK', 'false').downcase == 'true'
end

# Ensure storage directory exists
maxmind_storage_path = Rails.application.config.maxmind.storage_path
FileUtils.mkdir_p(maxmind_storage_path) unless Dir.exist?(maxmind_storage_path)