import 'package:cheminova/controller/home_service.dart';
import 'package:cheminova/models/user_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../notification_service.dart';

class HomeController extends GetxController {
  final HomeService homeService = HomeService();
  NotificationServices notificationServices = NotificationServices();

  UserModel? user;
  // var userModel = UserModel(
  //   id: '',
  //   uniqueId: '',
  //   name: '',
  //   email: '',
  //   phone: '',
  //   role: '',
  //   sbu: '',
  //   createdAt: '',
  //   updatedAt: '',
  // ).obs; // Observable for UserModel

  @override
  void onInit() {
    getUser();
    super.onInit();
    notificationServices.requestNotificationPermission();
    notificationServices.getDeviceToken().then((value) {
      print('Device Token: $value');
      fcmToken();
    });
  }

  Future<void> fcmToken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('token');
    final fcmToken = await NotificationServices().getDeviceToken();
    print('fcmToken: $fcmToken');
    homeService.fcmToken({"fcmToken": fcmToken}, token!);
  }

  Future<void> getUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('token');

    HomeService homeService = HomeService();
  user = await homeService.getUser(token: token);

    if (user != null) {
      print(user);  // For debugging, prints the user details
    } else {
      print('Failed to fetch user data');
    }
  }
}






// import 'package:cheminova/controller/home_service.dart';
// import 'package:cheminova/models/user_model.dart';
// import 'package:get/get.dart';
// import 'package:shared_preferences/shared_preferences.dart';
//
// import '../notification_service.dart';
//
// class HomeController extends GetxController {
//   final HomeService homeService = HomeService();
//   NotificationServices notificationServices = NotificationServices();
//
//
//
//   var userModel = UserModel(id: '', uniqueId: '', name: '', email: '', phone: '', role: '', sbu: '', createdAt: '', updatedAt: ''
//
//   ).obs; // Observable for UserModel
//
//   @override
//   void onInit() {
//     getUser();
//     super.onInit();
//     notificationServices.requestNotificationPermission();
//     notificationServices.getDeviceToken().then((value) {
//       print('Device Token: $value');
//       fcmToken();
//     });
//   }
//
//   Future<void> fcmToken() async {
//     SharedPreferences prefs = await SharedPreferences.getInstance();
//     String? token = prefs.getString('token');
//     final  fcmToken = await NotificationServices().getDeviceToken();
//     print('fcmToken: $fcmToken');
//     homeService.fcmToken({"fcmToken": fcmToken}, token!);
//   }
//
//   Future<void> getUser() async {
//     SharedPreferences prefs = await SharedPreferences.getInstance();
//
//     String? token = prefs.getString('token');
//
//      userModel = (await homeService.getUser(token: token)) as dynamic;
//
//
//     // if (userModel != null) {
//     // if (userModel != null) {ddddd
//     //   userModel.value = userResponse as UserModel; // Update the userModel with API response
//     //   update(); // Notify GetX to rebuild widgets using GetBuilder/Obx
//     // }
//   }
// }
//
