import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/registration_filter_model.dart';

import 'package:mitra_corporate/repository/surat_jalan_repository.dart';

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

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

class GetRegionalStateInit extends GetRegionalState {
  GetRegionalStateInit() : super(DateTime.now());
}

class GetRegionalStateLoading extends GetRegionalState {
  GetRegionalStateLoading() : super(DateTime.now());
}

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

class GetRegionalStateDone extends GetRegionalState {
  final List<RegionalModel> model;
  GetRegionalStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetRegionalNotifier extends StateNotifier<GetRegionalState> {
  final Ref ref;
  GetRegionalNotifier({
    required this.ref,
  }) : super(GetRegionalStateInit());

  void getRegional({required String token}) async {
    try {
      state = GetRegionalStateLoading();
      final dio = ref.read(dioProvider);
      final resp =
          await SuratJalanRepository(dio: dio).getRegional(token: token);
      state = GetRegionalStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetRegionalStateError(message: e.message.toString());
      } else {
        state = GetRegionalStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetRegionalProvider =
    StateNotifierProvider<GetRegionalNotifier, GetRegionalState>(
        (ref) => GetRegionalNotifier(ref: ref));
