import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/test_model.dart';
import 'package:mitra_corporate/model/user_mou_model.dart';
import 'package:mitra_corporate/repository/registration_repository.dart';

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

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

class GetUserMouStateInit extends GetUserMouState {
  GetUserMouStateInit() : super(DateTime.now());
}

class GetUserMouStateLoading extends GetUserMouState {
  GetUserMouStateLoading() : super(DateTime.now());
}

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

class GetUserMouStateDone extends GetUserMouState {
  final List<UserMouModel> model;
  GetUserMouStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetUserMouNotifier extends StateNotifier<GetUserMouState> {
  final Ref ref;
  GetUserMouNotifier({
    required this.ref,
  }) : super(GetUserMouStateInit());

  void getData({required String userID, required String token}) async {
    try {
      state = GetUserMouStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await RegistrationRepository(dio: dio)
          .getUserMou(userID: userID, token: token);
      state = GetUserMouStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetUserMouStateError(message: e.message.toString());
      } else {
        state = GetUserMouStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetUserMouProvider =
    StateNotifierProvider<GetUserMouNotifier, GetUserMouState>(
        (ref) => GetUserMouNotifier(ref: ref));
