import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/notifications/models/resp_models/notification_response_model.dart';
import 'package:hmg_patient_app_new/features/notifications/notifications_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class NotificationsViewModel extends ChangeNotifier {
  bool isNotificationsLoading = false;
  bool hasMoreNotifications = true;

  int unreadNotificationsCount = 0;

  NotificationsRepo notificationsRepo;
  ErrorHandlerService errorHandlerService;

  List<NotificationResponseModel> notificationsList = [];

  int currentPage = 0;
  int pagingSize = 50;
  int notificationStatusID = 2; // Default to status 2 (e.g., unread)

  NotificationsViewModel({
    required this.notificationsRepo,
    required this.errorHandlerService,
  });

  initNotificationsViewModel({int? statusID}) {
    notificationsList.clear();
    currentPage = 0;
    hasMoreNotifications = true;
    isNotificationsLoading = true;
    // if (statusID != null) {
    //   notificationStatusID = statusID;
    // }
    // getAllNotifications();
    notifyListeners();
  }

  setNotificationStatusID(int statusID) {
    notificationStatusID = statusID;
    notificationsList.clear();
    currentPage = 0;
    hasMoreNotifications = true;
    notifyListeners();
  }

  Future<void> getAllNotifications({
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    isNotificationsLoading = true;
    unreadNotificationsCount = 0;
    notifyListeners();

    final result = await notificationsRepo.getAllNotifications(
      notificationStatusID: notificationStatusID,
      pagingSize: pagingSize,
      currentPage: currentPage,
    );

    result.fold(
      (failure) async {
        isNotificationsLoading = false;
        notifyListeners();
        await errorHandlerService.handleError(failure: failure);
        if (onError != null) {
          onError(failure.toString());
        }
      },
      (apiResponse) {
        isNotificationsLoading = false;
        if (apiResponse.messageStatus == 2) {
          notifyListeners();
          if (onError != null) {
            onError(apiResponse.errorMessage ?? "Unknown error");
          }
        } else if (apiResponse.messageStatus == 1) {
          final newNotifications = apiResponse.data ?? [];

          if (newNotifications.isEmpty || newNotifications.length < pagingSize) {
            hasMoreNotifications = false;
          }

          notificationsList.addAll(newNotifications);
          for (var notification in notificationsList) {
            if (notification.isRead == false) {
              unreadNotificationsCount++;
            }
          }
          currentPage++;

          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> markAsRead({
    int notificationID = 0,
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    final result = await notificationsRepo.markAsRead(notificationID: notificationID);

    result.fold(
      (failure) async {},
      (apiResponse) {
        refreshNotifications();
      },
    );
  }

  Future<void> refreshNotifications({
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    notificationsList.clear();
    currentPage = 0;
    hasMoreNotifications = true;
    await getAllNotifications(onSuccess: onSuccess, onError: onError);
  }

  Future<void> loadMoreNotifications({
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    await getAllNotifications(onSuccess: onSuccess, onError: onError);
  }

  @override
  void dispose() {
    super.dispose();
  }
}
