import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow_core/core.dart';
import '../../domain/usecases/sign_out_usecase.dart';

part 'client_settings_event.dart';
part 'client_settings_state.dart';

/// BLoC to manage client settings and profile state.
class ClientSettingsBloc extends Bloc<ClientSettingsEvent, ClientSettingsState>
    with BlocErrorHandler<ClientSettingsState> {
  final SignOutUseCase _signOutUseCase;

  ClientSettingsBloc({required SignOutUseCase signOutUseCase})
    : _signOutUseCase = signOutUseCase,
      super(const ClientSettingsInitial()) {
    on<ClientSettingsSignOutRequested>(_onSignOutRequested);
  }

  Future<void> _onSignOutRequested(
    ClientSettingsSignOutRequested event,
    Emitter<ClientSettingsState> emit,
  ) async {
    emit(const ClientSettingsLoading());
    await handleError(
      emit: emit,
      action: () async {
        await _signOutUseCase();
        emit(const ClientSettingsSignOutSuccess());
      },
      onError: (String errorKey) => ClientSettingsError(errorKey),
    );
  }
}

