class SearchService < ApplicationService
  include ElasticsearchAble

  attr_reader :params

  def initialize(params)
    @params = params
  end

  def call
    Searchkick.search(keyword, search_options)
  end

  private

  def search_options
    {
      index_name: index_names,
      model_includes: model_includes
    }.merge(default_options)
  end

  def index_names
    @_index_names ||=
      case params[:type].to_s.strip
      when 'shixun'  then [Shixun]
      when 'course'  then [Course]
      when 'subject' then [Subject]
      when 'memo'    then [Memo]
      else [Shixun, Course, Subject, Memo]
      end
  end

  def model_includes
    index_names.each_with_object({}) do |klass, obj|
      obj[klass] = klass.searchable_includes
    end
  end
end