class School::Search::StudentsController < ApplicationController
  def index
    render_success :ok, json: students.map { |s| student_props(s) }
  end

  def show
    render_success :ok, json: student_props(student)
  end

  private
    def students
      current_school.students
        .search(params[:term], true)
        .current_status(current_school, params[:status]&.to_sym)
        .by_id(params[:student_ids])
        .by_family_ids(params[:family_ids])
        .by_grade(params[:grades], params[:not_grade]&.to_bool)
        .by_classroom(params[:classroom_ids])
        .by_billing_template(params[:billing_template_id], params[:without_template]&.to_bool)
        .distinct
        .ordered
    end

    def student
      @student ||= students.find_by(id: params[:id])
    end

    def student_props(student)
      {
        id: student.id,
        first_name: student.first_name,
        last_name: student.last_name,
        full_name: student.full_name(:reverse),
        grade: student.grade
      }
    end
end
