class Csv::AppleManager::RosterService < Csv::ApplicationService
  def initialize(school)
    @school = school
  end

  def call
    CSV.generate do |csv|
      csv << headers
      class_students.each do |class_student|
        next if missing_data?(class_student.student)

        csv << content(class_student)
      end
    end
  end

  private
   def class_students
      @school.class_students
        .preload(:student)
        .joins(classroom: :location)
        .where.not(Classes: { Course: ['', ' '] })

    end

    def missing_data?(student)
      [:first_name, :last_name, :location_id?].any? { |p| student.send(p).blank? }
    end

    def headers
      data = [
        'roster_id',
        'class_id',
        'student_id'
      ]
    end

    def content(class_student)
      [
        class_student.id,
        class_student.class_id,
        class_student.student_id
      ]
    end
end
