import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

import '../../../domain/booking_model.dart';
import '../../../domain/booking_service.dart';

part 'past_bookings_event.dart';
part 'past_bookings_state.dart';

class PastBookingsBloc extends Bloc<PastBookingsEvent, PastBookingsState> {
  final BookingService _bookingService;

  PastBookingsBloc(this._bookingService) : super(PastBookingsInitial()) {
    on<GetPastBookingsEvent>(_onGetPastBookingsEvent);
  }
  Future<void> _onGetPastBookingsEvent(
    GetPastBookingsEvent event,
    Emitter<PastBookingsState> emit,
  ) async {
    emit(PastBookingLoadingState());
    try {
      final pastBookings = await _bookingService.get();
      emit(
        PastBookingLoadedState(pastBookings: pastBookings),
      );
    } catch (e) {
      emit(
        PastBookingErrorState(e.toString()),
      );
    }
  }
}
