class NotifyStreamHealthJob
  include Sidekiq::Job

  def perform(session_id)
    session = StreamSession.find_by(id: session_id)
    return unless session&.live?

    camera = session.device_states.find_by(device_role: "camera")
    return unless camera&.current_bitrate && camera.current_bitrate < session.target_bitrate * 0.5

    SessionChannel.broadcast_message(session, {
      type: "stream_event",
      event: "quality_degraded",
      bitrate: camera.current_bitrate
    })
  end
end
