import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../core/contracts/server.dart';
import 'di_providers.dart';

class ServerConnectivity {
  final bool reachable;
  final int? latencyMs;

  const ServerConnectivity({required this.reachable, this.latencyMs});
}


final serverConnectivityProvider =
    FutureProvider.family<ServerConnectivity, String>((ref, baseUrl) async {
      final uc = await ref.read(probeServerUseCaseProvider.future);
      final sw = Stopwatch()..start();
      try {
        await uc.execute(EmbyServerProbeReq(baseUrl: baseUrl));
        sw.stop();
        return ServerConnectivity(
          reachable: true,
          latencyMs: sw.elapsedMilliseconds,
        );
      } catch (_) {
        sw.stop();
        return const ServerConnectivity(reachable: false);
      }
    });
