import 'package:mitra_corporate/repository/patient_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

abstract class DeletePatientProviderState extends Equatable {
  final DateTime date;
  const DeletePatientProviderState(this.date);
  @override
  List<Object?> get props => [date];
}

class DeletePatientProviderStateInit extends DeletePatientProviderState {
  DeletePatientProviderStateInit() : super(DateTime.now());
}

class DeletePatientProviderStateLoading extends DeletePatientProviderState {
  DeletePatientProviderStateLoading() : super(DateTime.now());
}

class DeletePatientProviderStateError extends DeletePatientProviderState {
  final String message;
  DeletePatientProviderStateError({
    required this.message,
  }) : super(DateTime.now());
}

class DeletePatientProviderStateDone extends DeletePatientProviderState {
  final String msg;
  DeletePatientProviderStateDone({
    required this.msg,
  }) : super(DateTime.now());
}

//notifier
class DeletePatientProviderNotifier
    extends StateNotifier<DeletePatientProviderState> {
  final Ref ref;
  DeletePatientProviderNotifier({
    required this.ref,
  }) : super(DeletePatientProviderStateInit());

  void DeletePatent({required String patient_id, required String token}) async {
    try {
      state = DeletePatientProviderStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await PatientRepository(dio: dio)
          .deletePatient(patient_id: patient_id, token: token);
      state = DeletePatientProviderStateDone(msg: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = DeletePatientProviderStateError(message: e.message.toString());
      } else {
        state = DeletePatientProviderStateError(message: e.toString());
      }
    }
  }
}

//provider
final DeletePatientProvider = StateNotifierProvider<
        DeletePatientProviderNotifier, DeletePatientProviderState>(
    (ref) => DeletePatientProviderNotifier(ref: ref));
