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_tables_info_usecase.dart';

abstract class PrivateInfoEvent {}

class LoadTablesInfoEvent extends PrivateInfoEvent {}

abstract class PrivateInfoState {}

class PrivateInfoInitial extends PrivateInfoState {}

class PrivateInfoLoading extends PrivateInfoState {}

class PrivateInfoLoaded extends PrivateInfoState {
  final TablesInfo info;
  PrivateInfoLoaded(this.info);
}

class PrivateInfoError extends PrivateInfoState {
  final String message;
  PrivateInfoError(this.message);
}

class PrivateInfoBloc extends Bloc<PrivateInfoEvent, PrivateInfoState> {
  final GetTablesInfoUseCase getTablesInfoUseCase;
  PrivateInfoBloc(this.getTablesInfoUseCase) : super(PrivateInfoInitial()) {
    on<LoadTablesInfoEvent>((event, emit) async {
      emit(PrivateInfoLoading());
      final res = await getTablesInfoUseCase(const NoParams());
      if (res is DataSuccess) {
        emit(PrivateInfoLoaded(res.data!));
      } else {
        emit(PrivateInfoError(res.error!));
      }
    });
  }
}
