// dart
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/appointment_details_resp_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

import 'models/resp_models/rate_appointment_resp_model.dart';

class AppointmentRatingViewModel extends ChangeNotifier {
  final MyAppointmentsRepo myAppointmentsRepo;
  final ErrorHandlerService errorHandlerService;
  final AppState appState;
  List<RateAppointmentRespModel> appointmentRatedList = [];
  AppointmentDetails? appointmentDetails;
  AppointmentRatingViewModel({
    required this.myAppointmentsRepo,
    required this.errorHandlerService,
    required this.appState,
  });


  String title = "";
  String subTitle = "";
  bool isRateClinic = false;

  Future<void> getLastRatingAppointment({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    appointmentRatedList.clear();
    final result = await myAppointmentsRepo.getLastRatingAppointment();

    result.fold(
          (failure) async => await errorHandlerService.handleError(failure: failure),
          (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          onError?.call(apiResponse.errorMessage ?? 'Unknown error');
        } else if (apiResponse.messageStatus == 1) {
          appointmentRatedList = apiResponse.data ?? [];
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse.data);
          }
        }
      },
    );
  }

  Future<void> getAppointmentDetails(int appointmentID, int projectID, {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.getAppointmentDetails(appointmentID, projectID);

    result.fold(
          (failure) async => await errorHandlerService.handleError(failure: failure),
          (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          onError?.call(apiResponse.errorMessage ?? 'Unknown error');
        } else if (apiResponse.messageStatus == 1) {
          appointmentDetails = apiResponse.data ?? AppointmentDetails();
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse.data);
          }
        }
      },
    );
  }
  Future<void> submitDoctorRating( {required int docRate, required String docNote,Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.sendDoctorRate(
       docRate,
      appointmentDetails!.appointmentNo!,
      appointmentDetails!.projectID!,
      appointmentDetails!.doctorID!,
      appointmentDetails!.clinicID!,
      docNote,
      appointmentDetails!.appointmentDate!,
        appointmentDetails!.doctorName,
        appointmentDetails!.projectName,
      appointmentDetails!.clinicName
        );

    result.fold(
          (failure) async => await errorHandlerService.handleError(failure: failure),
          (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          onError?.call(apiResponse.errorMessage ?? 'Unknown error');
        } else if (apiResponse.messageStatus == 1) {

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

  Future<void> submitClinicRating( { required int clinicRate, required String clinicNote, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.sendAppointmentRate(
        clinicRate,
        appointmentDetails!.appointmentNo!,
        appointmentDetails!.projectID!,
        appointmentDetails!.doctorID!,
        appointmentDetails!.clinicID!,
        clinicNote
    );

    result.fold(
          (failure) async => await errorHandlerService.handleError(failure: failure),
          (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          onError?.call(apiResponse.errorMessage ?? 'Unknown error');
        } else if (apiResponse.messageStatus == 1) {

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

  void setSubTitle(String value) {
    this.subTitle = value;
    notifyListeners();
  }

  void setTitle(String value) {
    this.title = value;
    notifyListeners();
  }
  void setClinicOrDoctor(bool value){
    this.isRateClinic = value;
    notifyListeners();
  }
}
