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

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

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

class EditPatientProviderStateInit extends EditPatientProviderState {
  EditPatientProviderStateInit() : super(DateTime.now());
}

class EditPatientProviderStateLoading extends EditPatientProviderState {
  EditPatientProviderStateLoading() : super(DateTime.now());
}

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

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

//notifier
class EditPatientProviderNotifier
    extends StateNotifier<EditPatientProviderState> {
  final Ref ref;
  EditPatientProviderNotifier({
    required this.ref,
  }) : super(EditPatientProviderStateInit());

  void editPatent({required RegistrationModel prm}) async {
    try {
      state = EditPatientProviderStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await PatientRepository(dio: dio).editPatient(prm: prm);
      state = EditPatientProviderStateDone(msg: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = EditPatientProviderStateError(message: e.message.toString());
      } else {
        state = EditPatientProviderStateError(message: e.toString());
      }
    }
  }
}

//provider
final EditPatientProviderProvider = StateNotifierProvider<
    EditPatientProviderNotifier,
    EditPatientProviderState>((ref) => EditPatientProviderNotifier(ref: ref));
