import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/history_model.dart';
import '../../repository/history_screen_repository.dart';
import '/screen/history_screen/history_screen.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 HistoryListState extends Equatable {
  final DateTime date;
  const HistoryListState(this.date);
  @override
  List<Object?> get props => [date];
}

class HistoryListStateInit extends HistoryListState {
  HistoryListStateInit() : super(DateTime.now());
}

class HistoryListStateLoading extends HistoryListState {
  HistoryListStateLoading() : super(DateTime.now());
}

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

class HistoryListStateDone extends HistoryListState {
  final List<HistoryModel> model;
  HistoryListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class HistoryListNotifier extends StateNotifier<HistoryListState> {
  final Ref ref;
  HistoryListNotifier({
    required this.ref,
  }) : super(HistoryListStateInit());

  void getData(
      {required String id, required String date, required String type}) async {
    try {
      state = HistoryListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await HistoryRepository(dio: dio)
          .getData(date: date, id: id, type: type);
      state = HistoryListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = HistoryListStateError(message: e.message.toString());
      } else {
        state = HistoryListStateError(message: e.toString());
      }
    }
  }
}

//provider
final HistoryListProvider =
    StateNotifierProvider<HistoryListNotifier, HistoryListState>(
        (ref) => HistoryListNotifier(ref: ref));
