import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/response_list_branch_model.dart';
import '/models/response_search_pengantaran_hasil_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/pengambilan_bahan_repository.dart';
import '../../repository/pengantaran_hasil_pasien_repository.dart';

// 3. state provider
final pengambilanBahanListCabang = StateNotifierProvider<PengambilanBahanNotifier, PengambilanBahanState>(
    (ref) => PengambilanBahanNotifier(ref: ref));

// 2. notifier
class PengambilanBahanNotifier extends StateNotifier<PengambilanBahanState> {
  final Ref ref;
  PengambilanBahanNotifier({required this.ref}) : super(PengambilanBahanStateInit());
  void loadListCabangPengambilanBahan() async {
    try {
      state = PengambilanBahanStateLoading();
      final resp = await PengambilanBahanRepository(dio: ref.read(dioProvider))
          .loadListCabangPengambilanBahan();

      state = PengambilanBahanStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PengambilanBahanStateError(message: e.message ?? "");
      } else {
        state = PengambilanBahanStateError(message: e.toString());
      }
    }
  }
}

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

class PengambilanBahanStateInit extends PengambilanBahanState {
  PengambilanBahanStateInit() : super(DateTime.now());
}

class PengambilanBahanStateLoading extends PengambilanBahanState {
  PengambilanBahanStateLoading() : super(DateTime.now());
}

class PengambilanBahanStateError extends PengambilanBahanState {
  final String? message;
  PengambilanBahanStateError({
    required this.message,
  }) : super(DateTime.now());
}

class PengambilanBahanStateDone extends PengambilanBahanState {
  final List<ResponseListBranchModel> model;
  PengambilanBahanStateDone({
    required this.model,
  }) : super(DateTime.now());
}
