import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
import 'package:hmg_patient_app_new/features/ask_doctor/ask_doctor_repo.dart';
import 'package:hmg_patient_app_new/features/ask_doctor/models/ask_doctor_appointments_list.dart';
import 'package:hmg_patient_app_new/features/ask_doctor/models/doctor_response_model.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class AskDoctorViewModel extends ChangeNotifier {
  final AskDoctorRepo askDoctorRepo;
  final ErrorHandlerService errorHandlerService;

  AskDoctorViewModel({
    required this.askDoctorRepo,
    required this.errorHandlerService,
  });

  bool isAskDoctorAppointmentsLoading = false;
  bool isDoctorResponsesLoading = false;

  List<AskDoctorAppointmentHistoryList> askDoctorAppointmentsList = [];
  List<DoctorResponseModel> doctorResponsesList = [];

  /// Initialize the view model
  void initAskDoctorViewModel() {
    askDoctorAppointmentsList.clear();
    doctorResponsesList.clear();
    isAskDoctorAppointmentsLoading = true;
    getAskDoctorAppointmentsList();
    notifyListeners();
  }

  Future<void> getAskDoctorAppointmentsList({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await askDoctorRepo.getAskDoctorAppointmentsList();

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          askDoctorAppointmentsList = apiResponse.data!;
          isAskDoctorAppointmentsLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  /// Get doctor responses from a specific date
  Future<void> getDoctorResponses({
    String? fromDate,
    Function(dynamic)? onSuccess,
    Function(String)? onError,
  }) async {
    isDoctorResponsesLoading = true;
    notifyListeners();

    // If no date provided, use current date minus 30 days
    final dateToUse = "${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day} ${DateTime.now().hour}:${DateTime.now().minute}:00";

    final result = await askDoctorRepo.getDoctorResponse(fromDate: dateToUse);

    result.fold(
      (failure) async {
        isDoctorResponsesLoading = false;
        notifyListeners();
        // await errorHandlerService.handleError(failure: failure);
        if (onError != null) {
          onError(failure.toString());
        }
      },
      (apiResponse) {
        isDoctorResponsesLoading = false;

        if (apiResponse.messageStatus == 2) {
          notifyListeners();
          if (onError != null) {
            onError(apiResponse.errorMessage ?? "Unknown error");
          }
        } else if (apiResponse.messageStatus == 1) {
          doctorResponsesList = apiResponse.data ?? [];
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }
}
