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

class WorkListStateInit extends WorkListState {
  WorkListStateInit() : super(DateTime.now());
}

class WorkListStateLoading extends WorkListState {
  WorkListStateLoading() : super(DateTime.now());
}

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

class WorkListStateDone extends WorkListState {
  final List<WorkListModel> model;
  WorkListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class WorkListNotifier extends StateNotifier<WorkListState> {
  final Ref ref;
  WorkListNotifier({
    required this.ref,
  }) : super(WorkListStateInit());

  void getData(
      {required String id,
      required String tipeId,
      required String supervisor,
      required String status}) async {
    try {
      state = WorkListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await WorkListRepository(dio: dio).getListWork(
          id: id, status: status, supervisor: supervisor, tipeId: tipeId);
      state = WorkListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = WorkListStateError(message: e.message.toString());
      } else {
        state = WorkListStateError(message: e.toString());
      }
    }
  }
}

//provider
final WorkListProvider = StateNotifierProvider<WorkListNotifier, WorkListState>(
    (ref) => WorkListNotifier(ref: ref));
