gsub_file! "config/environments/production.rb",
           "config.assume_ssl = true",
           <<~RUBY
             #
             # You should never use this because it just blindly sets headers without any actual
             # checks; instead, whatever is handling the SSL-termination should be setting the
             # appropriate headers to indicate that the request was actually SSL-terminated.
             config.assume_ssl = false
           RUBY

gsub_file! "config/environments/production.rb",
           "config.force_ssl = true",
           <<~RUBY
             #
             # On by default, though can be disabled by setting RAILS_FORCE_SSL=false (and only "false")
             config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "true").downcase != "false"
           RUBY

gsub_file! "config/environments/production.rb",
           "# config.action_mailer.raise_delivery_errors = false",
           "config.action_mailer.raise_delivery_errors = true"

gsub_file! "config/environments/production.rb",
           'config.action_mailer.default_url_options = { host: "example.com" }',
           <<~RUBY
             config.action_mailer.default_url_options = {
               host: "#{TEMPLATE_CONFIG.production_hostname}",
               protocol: "https"
             }

             config.action_mailer.asset_host = "https://#{TEMPLATE_CONFIG.production_hostname}"
           RUBY
gsub_file! "config/environments/production.rb",
           <<-RUBY,
  # Specify outgoing SMTP server. Remember to add smtp/* credentials via rails credentials:edit.
  # config.action_mailer.smtp_settings = {
  #   user_name: Rails.application.credentials.dig(:smtp, :user_name),
  #   password: Rails.application.credentials.dig(:smtp, :password),
  #   address: "smtp.example.com",
  #   port: 587,
  #   authentication: :plain
  # }
           RUBY
           <<-RUBY
  # Specify outgoing SMTP server.
  config.action_mailer.smtp_settings = {
    address: ENV.fetch("SMTP_HOSTNAME"),
    port: ENV.fetch("SMTP_PORT", 587),
    enable_starttls_auto: true,
    user_name: ENV.fetch("SMTP_USERNAME"),
    password: ENV.fetch("SMTP_PASSWORD"),
    authentication: "login",
    domain: "#{TEMPLATE_CONFIG.production_hostname}"
  }
RUBY

gsub_file! "config/environments/production.rb",
           'ENV.fetch("RAILS_LOG_LEVEL", "info")',
           'ENV.fetch("RAILS_LOG_LEVEL", ENV.fetch("LOG_LEVEL", "info"))'

gsub_file! "config/environments/production.rb",
           "ActiveSupport::TaggedLogging.logger(STDOUT)",
           "ActiveSupport::TaggedLogging.logger($stdout)"

gsub_file! "config/environments/production.rb",
           "config.silence_healthcheck_path =",
           "# config.silence_healthcheck_path ="

insert_into_file! "config/environments/production.rb",
                  after: /.*config.cache_store = :mem_cache_store\n/ do
  <<~RUBY
    if ENV.fetch("RAILS_CACHE_REDIS_URL", nil)
      config.cache_store = :redis_cache_store, {
        url: ENV.fetch("RAILS_CACHE_REDIS_URL"),
        ##
        # Configuring a connection pool for Redis as Rails cache is documented in:
        #
        # * https://edgeguides.rubyonrails.org/caching_with_rails.html#connection-pool-options
        #
        # but some more details are available in:
        #
        # * https://github.com/rails/rails/blob/a5d1628c79ab89dfae57ec1e1aeca467e29de188/activesupport/lib/active_support/cache.rb#L168-L173
        # * https://github.com/rails/rails/blob/9b4aef4be3dc58eb08f694387857b52be8050954/activesupport/lib/active_support/cache/redis_cache_store.rb#L185-L192
        #
        pool_size: Integer(ENV.fetch("RAILS_MAX_THREADS", 5)), # number of connections **per puma process**
        pool_timeout: 5 # num seconds to wait for a connection
      }
    end
  RUBY
end
