class Master::Devportal::AuthController < ApplicationController

  include SiteAccountSupport

  skip_before_action :verify_authenticity_token

  before_action :show_error

  def show
    account = Account.find_by!(domain: params.require(:domain))
    authentication_provider = account.authentication_providers.find_by!(system_name: params.require(:system_name))
    client = ThreeScale::OAuth2::Client.build(authentication_provider)
    domain = account.domain

    base_url = if params[:invitation_token].present?
                 ThreeScale::Domain.callback_endpoint(request, account, domain)
               else
                 ThreeScale::Domain.current_endpoint(request, domain)
    end

    query_parameters = request.query_parameters.except(:domain).merge(master: true)

    redirect_to client.callback_url(base_url, query_parameters)
  end

  protected

  AuthError = Struct.new(:message, :description, :uri)

  def show_error
    error = AuthError.new(*params.values_at(:error, :error_description, :error_uri))

    return unless error.message

    response.status = :unprocessable_entity
    render text: [error.message, error.description, error.uri].compact.join("\n")
  end
end
