import 'package:flutter_bloc/flutter_bloc.dart';
import '../../local_peference/local_preference.dart'; // TODO: update path if needed
part 'splash_event.dart';
part 'splash_state.dart';

class SplashBloc extends Bloc<SplashEvent, SplashState> {
  SplashBloc() : super(SplashInitial()) {
    on<SplashStarted>(_onSplashStarted);
  }

  Future<void> _onSplashStarted(
      SplashStarted event,
      Emitter<SplashState> emit,
      ) async {
    emit(SplashLoading());

    await Future.delayed(const Duration(seconds: 3));

    final bool isLoggedIn = await LocalPreference.getLogin(); // ← CHECK LOGIN FIRST

    if (isLoggedIn) {
      emit(SplashNavigateToHome()); // ← already logged in → go to QR
      return;
    }

    final bool showOnboarding = await LocalPreference.getOnBoarding();

    if (showOnboarding) {
      emit(SplashNavigateToOnboarding());
    } else {
      emit(SplashNavigateToLogin());
    }
  }
}