import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../auth/auth_provider.dart';
import 'hub_client.dart';

/// Keeps a single live hub connection alive while the user is authenticated.
final hubClientProvider = Provider<WaiterHubClient?>((ref) {
  final session = ref.watch(authProvider);
  if (session == null) return null;

  final client = WaiterHubClient(
    cafeId: session.cafeId,
    accessToken: session.accessToken,
  );

  // Connect asynchronously; provider consumers will react to stream events.
  client.connect();

  ref.onDispose(client.dispose);

  return client;
});
