class HomeworkEvaluationStartNotifyJob < ApplicationJob
  queue_as :notify

  def perform(homework_common_id, content)
    homework = HomeworkCommon.find_by(id: homework_common_id)
    return if homework.blank?
    course = homework.course
    members = content.blank? ? course.course_members : course.teachers
    tiding_type = content.blank? ? "HomeworkCommon" : "System"

    attrs = %i[
      user_id trigger_user_id container_id container_type parent_container_id parent_container_type
      belong_container_id belong_container_type viewed tiding_type extra created_at updated_at
    ]

    same_attrs = {
        trigger_user_id: homework.user_id, container_id: homework.id, container_type: 'HomeworkCommon',
        parent_container_id: homework.id, parent_container_type: 'AnonymousComment',
        belong_container_id: homework.course_id, belong_container_type: 'Course',
        viewed: 0, tiding_type: tiding_type, extra: content
    }
    Tiding.bulk_insert(*attrs) do |worker|
      member_ids = members.pluck(:user_id).uniq

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