class AppHost
  attr_reader :env_vars, :current_env

  def initialize(env_vars = ENV, current_env = Rails.env)
    @env_vars = env_vars
    @current_env = current_env
  end

  def domain
    env_vars.fetch("DOMAIN") { default_domain }
  end

  def port
    env_vars.fetch("WEB_PORT") { default_port }
  end

  def domain_with_port
    [domain, port].compact.join(":")
  end

  def protocol
    using_ssl? ? :https : :http
  end

  def using_ssl?
    env_vars.fetch("USE_SSL") { default_use_ssl }
  end

  private

  DEFAULTS = {
    development: {
      host: "localhost",
      port: 3000,
      use_ssl: false,
    },
    test: {
      host: "localhost",
      port: 31337,
      use_ssl: false,
    },
    production: {
      host: "bigmedia.ai",
      use_ssl: true,
    }
  }

  def default_domain
    DEFAULTS.dig(current_env.to_sym, :host)
  end

  def default_port
    DEFAULTS.dig(current_env.to_sym, :port)
  end

  def default_use_ssl
    DEFAULTS.dig(current_env.to_sym, :use_ssl)
  end
end
