import 'package:cheminova/models/notification_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'notification_api_service.dart';

class NotificationController extends GetxController {
  var isLoading = false.obs;
  var notificationList = <NotificationModel>[].obs;

  final NotificationService _notificationApiService = NotificationService();

  // Function to fetch notifications from the API with the selected date
  void fetchNotificationApiService(String date) async {
    try {
      isLoading(true);
      // Retrieve token from a secure source
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');

      if (token == null) {
        print("Token is null");
        return;
      }

      // Fetch notifications by date
      List<NotificationModel>? notifications = await _notificationApiService.fetchNotifications(token, date);

      if (notifications != null && notifications.isNotEmpty) {
        notificationList.value = notifications;
      } else {
        notificationList.clear();
      }
    } catch (e) {
      print("Error fetching notifications: $e");
    } finally {
      isLoading(false);
    }
  }
}
