import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/booking_system/domain/booking_model.dart';
import 'package:syncrow_app/features/booking_system/domain/booking_service.dart';

part 'upcoming_bookings_event.dart';
part 'upcoming_bookings_state.dart';

class UpcomingBookingsBloc
    extends Bloc<UpcomingBookingsEvent, UpcomingBookingsState> {
  final BookingService _bookingService;
  UpcomingBookingsBloc(this._bookingService)
      : super(UpcomingBookingsInitial()) {
    on<GetUpcomingBookingsEvent>(_onGetUpcomingBookingsEvent);
  }
  Future<void> _onGetUpcomingBookingsEvent(
    GetUpcomingBookingsEvent event,
    Emitter<UpcomingBookingsState> emit,
  ) async {
    emit(UpcomingBookingLoadingState());
    try {
      final upcomingBookings = await _bookingService.get();
      emit(
        UpcomingBookingLoadedState(upcomingBookings: upcomingBookings),
      );
    } catch (e) {
      emit(
        UpcomingBookingErrorState(e.toString()),
      );
    }
  }
}
