class Admins::ImportHackQuery < ApplicationQuery
  include CustomSortable

  attr_reader :params

  sort_columns :created_at, default_by: :created_at, default_direction: :desc, default_table: "item_banks"

  def initialize(params)
    @params = params
  end

  def call
    item_banks = ItemBank.PROGRAM

    if params[:sub].present?
      item_banks = item_banks.joins(:sub_discipline).where("sub_disciplines.name like ?", "%#{params[:sub]}%")
    end
    if params[:public].present?
      item_banks = item_banks.where(public: params[:public])
    end
    if params[:name].present?
      item_banks = item_banks.where('item_banks.name like ?', "%#{params[:name]}%")
    end
    if params[:user].present?
      item_banks = item_banks.joins(:user).where('lastname like ?', "%#{params[:user]}%")
    end
    custom_sort(item_banks, params[:sort_by], params[:sort_direction])
  end
end