class CourseVideoUploadedJob < ApplicationJob
  queue_as :notify

  def perform(video_id)
    video = Video.select("id, user_id").find_by(id: video_id)
    course_ids = video&.course_videos&.pluck(:course_id)

    return unless course_ids.present?

    course_members = CourseMember.where(course_id: course_ids, role: 'STUDENT').select("id, user_id, course_id")
    Tiding.bulk_insert do |worker|
      course_members.find_each do |m|
        worker.add(
          user_id:               m.user_id,
          tiding_type:           'PublishCourseVideo',
          trigger_user_id:       video.user_id,
          container_id:          video.id,
          container_type:        'Video',
          belong_container_type: 'Course',
          belong_container_id:   m.course_id)
      end
    end
  end

end
