import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/monthly_report/monthly_report_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class MonthlyReportViewModel extends ChangeNotifier {
  MonthlyReportRepo monthlyReportRepo;
  ErrorHandlerService errorHandlerService;

  bool isUpdateHealthSummaryLoading = false;
  bool isHealthSummaryEnabled = false;

  MonthlyReportViewModel({
    required this.monthlyReportRepo,
    required this.errorHandlerService,
  });

  setHealthSummaryEnabled(bool value) {
    isHealthSummaryEnabled = value;
    notifyListeners();
  }

  Future<void> updatePatientHealthSummaryReport({
    required bool rSummaryReport,
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    isUpdateHealthSummaryLoading = true;
    notifyListeners();

    final result = await monthlyReportRepo.updatePatientHealthSummaryReport(
      rSummaryReport: rSummaryReport,
    );

    result.fold(
      (failure) async {
        isUpdateHealthSummaryLoading = false;
        notifyListeners();
        await errorHandlerService.handleError(failure: failure);
        if (onError != null) {
          onError(failure.toString());
        }
      },
      (apiResponse) {
        isUpdateHealthSummaryLoading = false;
        if (apiResponse.messageStatus == 2) {
          notifyListeners();
          if (onError != null) {
            onError(apiResponse.errorMessage ?? "Unknown error");
          }
        } else if (apiResponse.messageStatus == 1) {
          // Update the local state on success
          isHealthSummaryEnabled = rSummaryReport;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

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