class Pdf::Admissions::StudentService < Pdf::NewApplicationService
  def initialize(school, options)
    @school = school
    @options = options
  end

  private
    def body
      ActionController::Base.new.render_to_string(
        partial: 'pdf/admissions/students.html.haml',
        locals: { students: students, grades: grades }
      )
    end

    def configuration
      {}.tap do |config|
        config[:margin] = { top: 10, bottom: 20 }
      end
    end

    def footer
      content = ActionController::Base.new.render_to_string(
        partial: 'pdf/footers/page_number.html.haml',
        locals: { side_content: "#{students.size} students" }
      )
      { content: content, spacing: 5 }
    end

    def school_year
      @school_year ||= @school.school_years.find(@options['school_year_id'])
    end

    def students
      @students ||= @school.students
        .preload(:family)
        .join_school_year_student(school_year.id, @options['status'].to_sym)
        .by_applicant(@school, @options['is_applicant']&.to_bool)
        .distinct
        .ordered
    end

    def grades
      @grades ||= @school.grades_hash
    end
end
