modules/representation_management/app/controllers/representation_management/v0/next_steps_email_controller.rb
# frozen_string_literal: true

module RepresentationManagement
  module V0
    class NextStepsEmailController < ApplicationController
      service_tag 'representation-management'
      skip_before_action :authenticate
      before_action :feature_enabled

      # Creates and enqueues an email with the provided "next steps" information. This action
      # validates the input parameters and, if valid, queues an email using the VANotify service.
      #
      # @return [JSON] Returns a success message if the email is enqueued, otherwise returns validation errors.
      #
      def create
        data = RepresentationManagement::NextStepsEmailData.new(next_steps_email_params)
        if data.valid?
          VANotify::EmailJob.perform_async(
            data.email_address,
            template_id,
            email_personalisation(data),
            Settings.vanotify.services.va_gov.api_key,
            email_callback_options(data)
          )
          render json: { message: 'Email enqueued' }, status: :ok
        else
          render json: { errors: data.errors.full_messages }, status: :unprocessable_entity
        end
      end

      private

      def email_personalisation(data)
        {
          'first_name' => data.first_name,
          'form name' => data.form_name,
          'form number' => data.form_number,
          'representative type' => data.entity_display_type,
          'representative name' => data.entity_name,
          'representative address' => data.entity_address
        }
      end

      def feature_enabled
        routing_error unless Flipper.enabled?(:appoint_a_representative_enable_pdf)
      end

      def email_callback_options(data)
        return unless Flipper.enabled?(:accredited_representative_portal_email_delivery_callback)

        email_delivery_callback(data)
      end

      def email_delivery_callback(data)
        {
          callback_klass: 'AccreditedRepresentativePortal::EmailDeliveryStatusCallback',
          callback_metadata: {
            form_number: data.form_number,
            statsd_tags: {
              service: 'representation-management',
              function: 'appoint_a_representative_confirmation_email'
            }
          }
        }
      end

      # Strong parameters method for sanitizing input data for the next steps email.
      #
      # @return [ActionController::Parameters] Filtered parameters permitted for the next steps email.
      def next_steps_email_params
        params.require(:next_steps_email).permit(
          :email_address,
          :first_name,
          :form_name,
          :form_number,
          :entity_type,
          :entity_id
        )
      end

      def template_id
        Settings.vanotify.services.va_gov.template_id.appoint_a_representative_confirmation_email
      end
    end
  end
end
