import 'package:wanisdriver/models/users.dart'; import 'package:wanisdriver/services/driver_location_service_manager.dart'; import 'package:wanisdriver/services/ride_socket_service_manager.dart'; import 'package:flutter/foundation.dart'; import '../Provider/public_keys_provider.dart'; class AppBackgroundServices { static Future startAllServices({ required users user, required String authToken, }) async { if (user.driver == null || user.driver!.id == null || user.username == null) { debugPrint('❌ Cannot start services: User data is incomplete.'); return; } final int driverId = user.driver!.id!; final String username = user.username!; debugPrint('🚀 Initializing background services for driver ID: $driverId...'); // 1. Start Ride Listening Service (PubNub) try { if (!publickeys.guardKey(publickeys.pubnubPublishKey, 'pubnubPublishKey') || !publickeys.guardKey(publickeys.pubnubSubscribeKey, 'pubnubSubscribeKey')) { debugPrint('❌ PubNub keys not configured — Ride Listening Service not started'); } else { await RideSocketServiceManager.startService( username: username, pubKey: publickeys.pubnubPublishKey!, subKey: publickeys.pubnubSubscribeKey!, ); debugPrint('✅ Ride Listening Service (PubNub) started/updated.'); } } catch (e) { debugPrint('❌ Failed to start Ride Listening Service: $e'); } // 2. Start Location Sending Service try { await DriverLocationServiceManager.startService( authToken: authToken, driverId: driverId, ); debugPrint('✅ Driver Location Service started.'); } catch (e) { debugPrint('❌ Failed to start Driver Location Service: $e'); } } static Future stopAllServices() async { debugPrint('🛑 Stopping all background services...'); await RideSocketServiceManager.stopService(); await DriverLocationServiceManager.stopService(); debugPrint('✅ All background services stopped.'); } }