import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/personal_information_model.dart';
import '../../repository/personal_information_repository.dart';
import '../../repository/work_list_repository.dart';

import '../../models/pending_work_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/home_screen_repository.dart';

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

class PersonalInformationStateInit extends PersonalInformationState {
  PersonalInformationStateInit() : super(DateTime.now());
}

class PersonalInformationStateLoading extends PersonalInformationState {
  PersonalInformationStateLoading() : super(DateTime.now());
}

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

class PersonalInformationStateDone extends PersonalInformationState {
  final List<PersonalInformationModel> model;
  PersonalInformationStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class PersonalInformationNotifier extends StateNotifier<PersonalInformationState> {
  final Ref ref;
  PersonalInformationNotifier({
    required this.ref,
  }) : super(PersonalInformationStateInit());

  void getData(
      {required String mCourierID}) async {
    try {
      state = PersonalInformationStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await PersonalInformationRepository(dio: dio).getPersonalInformation(mCourierID: mCourierID);
      state = PersonalInformationStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PersonalInformationStateError(message: e.message.toString());
      } else {
        state = PersonalInformationStateError(message: e.toString());
      }
    }
  }
}

//provider
final PersonalInformationProvider = StateNotifierProvider<PersonalInformationNotifier, PersonalInformationState>(
    (ref) => PersonalInformationNotifier(ref: ref));
