class JamRuby::LessonSession
  ransacker :student_full_name, formatter: proc { |v| v.mb_chars.downcase.to_s } do |parent|
    Arel::Nodes::NamedFunction.new('LOWER',
                                   [Arel::Nodes::NamedFunction.new('concat_ws',
                                                                   [Arel::Nodes::SqlLiteral.new("' '"), User.arel_table[:first_name], User.arel_table[:last_name]])])
  end

  ransacker :teacher_full_name, formatter: proc { |v| v.mb_chars.downcase.to_s } do |parent|
    Arel::Nodes::NamedFunction.new('LOWER',
                                   [Arel::Nodes::NamedFunction.new('concat_ws',
                                                                   [Arel::Nodes::SqlLiteral.new("' '"), 'teachers.first_name', 'teachers.last_name'])])
  end

  ransacker :by_search_teacher,
            formatter: proc { |teacher_name|
              ilikey = "%#{teacher_name}%"
              results = LessonSession.joins(:teacher).where('(users.email ilike ?) OR (LOWER(concat_ws(\' \', "users"."first_name", "users"."last_name")) ilike ?)', ilikey, ilikey).map(&:id)
              results = results.present? ? results : nil
            }, splat_params: true do |parent|
    parent.table[:id]
  end

  ransacker :by_lesson_package_type,
            formatter: proc { |lesson_package_type_id|
              results = LessonSession.joins(:lesson_booking => [:posa_card]).where('posa_cards.lesson_package_type_id = ? ', lesson_package_type_id).map(&:id)
              results = results.present? ? results : nil
            }, splat_params: true do |parent|
    parent.table[:id]

  end
end

