import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../repository/filter_repository.dart';

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

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

class WorkSupervisorStateInit extends WorkSupervisorState {
  WorkSupervisorStateInit() : super(DateTime.now());
}

class WorkSupervisorStateLoading extends WorkSupervisorState {
  WorkSupervisorStateLoading() : super(DateTime.now());
}

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

class WorkSupervisorStateDone extends WorkSupervisorState {
  final List<WorkSupervisorModel> model;
  WorkSupervisorStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class WorkSupervisorNotifier extends StateNotifier<WorkSupervisorState> {
  final Ref ref;
  WorkSupervisorNotifier({
    required this.ref,
  }) : super(WorkSupervisorStateInit());

  void getData() async {
    try {
      state = WorkSupervisorStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await FilterRepository(dio: dio).getSupervisor();
      state = WorkSupervisorStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = WorkSupervisorStateError(message: e.message.toString());
      } else {
        state = WorkSupervisorStateError(message: e.toString());
      }
    }
  }
}

//provider
final WorkSupervisorProvider =
    StateNotifierProvider<WorkSupervisorNotifier, WorkSupervisorState>(
        (ref) => WorkSupervisorNotifier(ref: ref));
