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 '../config/constants/app_constant.dart'; class AppBackgroundServices { // ✅ AUTH TOKEN IS NOW A REQUIRED PARAMETER static Future startAllServices({ required users user, required String authToken, }) async { // Ensure we have a driver profile if (user.driver == null || user.driver!.id == null || user.username == null) { debugPrint('❌ Cannot start services: User data is incomplete.'); return; } // ✅ USE THE PARAMETERS DIRECTLY 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 { await RideSocketServiceManager.startService( username: username, pubKey: pubKey, subKey: subKey, ); debugPrint('✅ Ride Listening Service (PubNub) started/updated.'); } catch (e) { debugPrint('❌ Failed to start Ride Listening Service: $e'); } // 2. Start Location Sending Service try { // ✅ NO MORE SharedPreferences. WE USE THE PASSED-IN TOKEN. 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.'); } }