class StreamAnalyticsJob
  include Sidekiq::Job

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

    duration = session.total_duration_secs
    disconnects = session.disconnection_count
    session.stream_events.create!(
      event_type: "quality_changed",
      metadata: { analytics: true, duration: duration, disconnects: disconnects },
      occurred_at: Time.current
    )
  end
end
