class CreateStudentWorkJob < ApplicationJob
  queue_as :default

  def perform(homework_id)
    homework = HomeworkCommon.find_by(id: homework_id)
    course = homework&.course
    return if homework.blank? || course.blank?

    attrs = %i[homework_common_id user_id created_at updated_at]

    same_attrs = {homework_common_id: homework.id}

    StudentWork.bulk_insert(*attrs) do |worker|
      student_ids = course.students.pluck(:user_id)

      student_ids.each do |user_id|
        worker.add same_attrs.merge(user_id: user_id)
      end
    end
  end
end
