# frozen_string_literal: true

module Decidim
  module Admin
    class RemindersController < Admin::ApplicationController
      include Decidim::ComponentPathHelper

      helper_method :reminder_manifest

      def new
        enforce_permission_to :create, :reminder

        @form = reminder_form_from_params(name: reminder_manifest.name)
        render :new
      end

      def create
        enforce_permission_to :create, :reminder

        @form = reminder_form_from_params(params)

        command_class.call(@form) do
          on(:ok) do |reminders_queued|
            flash[:notice] = t("decidim.admin.reminders.create.success", count: reminders_queued)
            redirect_to manage_component_path(current_component)
          end

          on(:invalid) do
            flash.now[:alert] = t("decidim.admin.reminders.create.error")
            render :new, status: :unprocessable_entity
          end
        end
      end

      private

      def reminder_form_from_params(params)
        form(reminder_manifest.form_class).from_params(
          params,
          current_component:
        )
      end

      def reminder_manifest
        @reminder_manifest ||= Decidim.reminders_registry.for(reminder_name)
      end

      def reminder_name
        params[:name]
      end

      def command_class
        reminder_manifest.command_class
      end

      def current_component
        @current_component ||= current_participatory_space.components.find(params[:component_id])
      end
    end
  end
end
