import 'dart:io';

import 'package:flutter/foundation.dart';

class NetworkConnectivity {
  static final NetworkConnectivity _instance = NetworkConnectivity._internal();
  factory NetworkConnectivity() => _instance;
  NetworkConnectivity._internal();

  /// Check if device has internet connectivity
  Future<bool> hasInternetConnection() async {
    if (kIsWeb) {
      return true;
    } else {
      try {
        // Try to reach a reliable host to check internet connectivity
        final result = await InternetAddress.lookup('google.com');
        return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
      } on SocketException catch (_) {
        return false;
      } on Exception catch (_) {
        // If connectivity check fails, assume no connection
        return false;
      }
    }
  }
}
