# frozen_string_literal: true

class DonateController < ApplicationController
  layout "bootstrap"

  before_action do
    @responsive = true
    @footless = true
    @no_footer_gap = true
    @shareable_image_url = helpers.image_url( "donate-banner.png" )
  end

  def index
    new_params = redirect_params
    redirect_to donate_url( new_params ) if new_params
  end

  def monthly_supporters
    new_params = redirect_params
    redirect_to monthly_supporters_url( new_params ) if new_params
  end

  def fundraiseup; end

  private

  def redirect_params
    # if there is a redirect param then an attempt has already been made to
    # include utm params on redirect, so do not attempt to redirect again
    return nil if params[:redirect]

    # Ensure utm_source is set to the site domain, defaulting to the default
    # site's domain. Note that donorbox will not save *any* utm_ values unless
    # utm_source is not blank
    utm_source = begin
      URI.parse( @site.domain )&.host || @site.domain
    rescue URI::InvalidURIError
      nil
    end
    utm_source ||= URI.parse( Site.default.domain )&.host || Site.default.domain
    default_site_uri = URI.parse( Site.default.url )
    if Site.default && @site && @site.id != Site.default.id
      {
        protocol: default_site_uri.scheme,
        host: default_site_uri.host,
        port: default_site_uri.port,
        utm_source: utm_source,
        redirect: true
      }.merge( request.query_parameters.reject {| k, _v | k.to_s == "inat_site_id" } )
    elsif params[:utm_source].blank?
      # We're doing this because the donorbox iframe only seems to derive utm
      # params from the URL of the parent window, and will ignore them all if
      # utm_source isn't set
      {
        protocol: default_site_uri.scheme,
        host: default_site_uri.host,
        port: default_site_uri.port,
        utm_source: utm_source,
        utm_medium: "web",
        redirect: true
      }.merge( request.query_parameters.reject {| k, _v | k.to_s == "inat_site_id" }.symbolize_keys )
    end
  end
end
