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

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

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

class GetFPPStateInit extends GetFPPState {
  GetFPPStateInit() : super(DateTime.now());
}

class GetFPPStateLoading extends GetFPPState {
  GetFPPStateLoading() : super(DateTime.now());
}

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

class GetFPPStateDone extends GetFPPState {
  final List<TestModel> model;
  GetFPPStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetFPPNotifier extends StateNotifier<GetFPPState> {
  final Ref ref;
  GetFPPNotifier({
    required this.ref,
  }) : super(GetFPPStateInit());

  void getData({required String mouID, required String token}) async {
    try {
      state = GetFPPStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await RegistrationRepository(dio: dio)
          .getFPP(mouID: mouID, token: token);
      state = GetFPPStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetFPPStateError(message: e.message.toString());
      } else {
        state = GetFPPStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetFPPProvider = StateNotifierProvider<GetFPPNotifier, GetFPPState>(
    (ref) => GetFPPNotifier(ref: ref));
