# 直播开启的消息通知
class LivePublishJob < ApplicationJob
  queue_as :notify

  def perform(live_id)
    live = LiveLink.find_by(id: live_id)
    return if live.blank? || live.course.blank?
    course = live.course

    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 created_at updated_at
    ]

    same_attrs = {
      trigger_user_id: live.user_id, container_id: live.id, container_type: 'LiveLink',
      parent_container_id: live.id, parent_container_type: 'LiveLink',
      belong_container_id: live.course_id, belong_container_type: 'Course',
      viewed: 0, tiding_type: 'LiveLink'
    }
    Tiding.bulk_insert(*attrs) do |worker|
      course.students.pluck(:user_id).each do |user_id|
        worker.add same_attrs.merge(user_id: user_id)
      end
    end
  end
end
