class Admin::Legacy::Nursing::StudentsController < Admin::Legacy::Nursing::Controller
  def index
    data = students
      .pluck(:id, :first_name, :last_name, :code, :grade)
      .map do |id, first, last, code, grade|
        first, last = encode_strings_to_utf8([first, last])
        {
          id: id,
          full_name: "#{last}, #{first}",
          code: code,
          grade_level: grade,
          grade_label: grade_levels[grade]
        }
      end

    render_success :ok, json: data
  end

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

  def emergency_info
    Reporting::Nursing::EmergencyInfoJob.perform_async(
      current_school.id,
      current_user.id,
      ids: params[:ids] || students.pluck(:id)
    )
    render_success :ok
  end

  def immunizations
    Reporting::Nursing::ImmunizationsJob.perform_async(
      current_school.id,
      current_user.id,
      ids: params[:ids] || students.pluck(:id)
    )
    render_success :ok
  end

  def medical_alerts
    Reporting::Nursing::MedicalAlertsJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id)
    )
    render_success :ok
  end

  def allergies
    Reporting::Nursing::AllergiesJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id)
    )
    render_success :ok
  end

  def exempt
    Reporting::Nursing::ExemptJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id)
    )
    render_success :ok
  end

  def missing_vaccinations
    Reporting::Nursing::MissingVaccinationJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id)
    )
    render_success :ok
  end

  def vaccination_export
    Reporting::Nursing::StudentVaccinationJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id)
    )
  end

  def screening_export
    Reporting::Nursing::ScreeningJob.perform_async(
      current_school.id,
      current_user.id,
      ids: students.pluck(:id),
      dates: params[:dates]
    )
  end

  def vaccine_summary
    Reporting::Nursing::VaccineSummaryJob.perform_async(current_school.id, current_user.id)
  end

  def logs
    Reporting::Nursing::StudentLogsJob.perform_async(
      current_school.id,
      current_user.id,
      dates: params[:dates],
      student_id: params[:student_id]
    )
  end

  private
    def students
      current_school.students
        .current_status(current_school, params[:status]&.to_sym)
        .has_vaccine_exemption(params[:exempt]&.to_bool)
        .by_grade(params[:grades])
        .by_classroom(params[:classrooms])
        .distinct
    end

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

    def student_props(student)
      {}.tap do |props|
        props[:id] = student.id
        props[:full_name] = student.full_name(:reverse)
        props[:code] = student.code
        props[:grade_label] = grade_levels[student.grade]
        props[:grade_level] = student.grade
        return props unless action_name.to_sym == :show

        props[:first_name] = student.first_name
        props[:middle_name] = student.middle_name
        props[:last_name] = student.last_name
        props[:nickname] = student.nickname&.first_name
        props[:date_of_birth] = student.date_of_birth
        props[:photo_url] = student.path_to_photo
        props[:gender_label] = student.decorate.gender_label
        props[:age] = student.decorate.age
      end
    end
end
