# frozen_string_literal: true

module Decidim
  module Sortitions
    # Exposes the sortition resource so users can view them
    class SortitionsController < Decidim::Sortitions::ApplicationController
      include FilterResource
      include Decidim::Sortitions::Orderable
      include Paginable

      helper_method :sortition

      helper Decidim::Proposals::ApplicationHelper

      def index
        @sortitions = search
                      .result
                      .includes(:taxonomies)

        @sortitions = reorder(@sortitions)
        @sortitions = paginate(@sortitions)
      end

      def show
        raise ActionController::RoutingError, "Not Found" unless sortition
      end

      private

      def sortition
        search_collection.find_by(id: params[:id])
      end

      def search_collection
        Sortition.where(component: current_component)
      end

      def default_filter_params
        {
          search_text_cont: "",
          with_any_taxonomies: nil,
          with_any_state: "active"
        }
      end
    end
  end
end
