import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../core/resources/data_state.dart';
import '../../../../core/usecase/use_case.dart';
import '../../domain/entities/table_entities.dart';
import '../../domain/use_cases/get_tiers_usecase.dart';

abstract class TierEvent {}

class LoadTiersEvent extends TierEvent {}

abstract class TierState {}

class TierInitial extends TierState {}

class TierLoading extends TierState {}

class TierLoaded extends TierState {
  final List<TableTier> tiers;
  TierLoaded(this.tiers);
}

class TierError extends TierState {
  final String message;
  TierError(this.message);
}

class TierBloc extends Bloc<TierEvent, TierState> {
  final GetTiersUseCase getTiersUseCase;
  TierBloc(this.getTiersUseCase) : super(TierInitial()) {
    on<LoadTiersEvent>((event, emit) async {
      emit(TierLoading());
      final res = await getTiersUseCase(const NoParams());
      if (res is DataSuccess) {
        emit(TierLoaded(res.data!));
      } else {
        emit(TierError(res.error!));
      }
    });
  }
}
