import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/update_space/domain/services/update_space_service.dart';
import 'package:syncrow_web/services/api/api_exception.dart';

part 'update_space_event.dart';
part 'update_space_state.dart';

class UpdateSpaceBloc extends Bloc<UpdateSpaceEvent, UpdateSpaceState> {
  final UpdateSpaceService _updateSpaceService;

  UpdateSpaceBloc(this._updateSpaceService) : super(UpdateSpaceInitial()) {
    on<UpdateSpace>(_onUpdateSpace);
  }

  Future<void> _onUpdateSpace(
    UpdateSpace event,
    Emitter<UpdateSpaceState> emit,
  ) async {
    emit(UpdateSpaceLoading());
    try {
      final updatedSpace = await _updateSpaceService.updateSpace(event.space);
      emit(UpdateSpaceSuccess(updatedSpace));
    } on APIException catch (e) {
      emit(UpdateSpaceFailure(e.message));
    } catch (e) {
      emit(UpdateSpaceFailure(e.toString()));
    }
  }
}
