import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'dio_provider.dart';
import '../repository/aliases_repository.dart';
import '../repository/base_repository.dart';
import '../model/aliases_model.dart';


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

class AliasesStateInit extends AliasesState {
  AliasesStateInit() : super(DateTime.now());
}

class AliasesStateLoading extends AliasesState {
  AliasesStateLoading() : super(DateTime.now());
}

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

class AliasesStateDone extends AliasesState {
  final List<AliasesModel> model;
  AliasesStateDone({
    required this.model,
  }) : super(DateTime.now());
}

class AliasesNotifier extends StateNotifier<AliasesState> {
  final Ref ref;
  AliasesNotifier({
    required this.ref,
  }) : super(AliasesStateInit());

  void getAliases({required String token}) async {
    try {
      state = AliasesStateLoading();
      final resp = await AliasesRepository(dio: ref.read(dioProvider)).getAliases(token: token);
      state = AliasesStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = AliasesStateError(message: e.message ?? "");
      } else {
        state = AliasesStateError(message: e.toString());
      }
    }
  }
}

final aliasesDataProvider = StateNotifierProvider<AliasesNotifier, AliasesState>((ref) => AliasesNotifier(ref: ref));