import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../profile/repository/profile_repository.dart';
import 'profile_event.dart';
import 'profile_state.dart';

class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
  final ProfileRepository _profileRepository;

  ProfileBloc({required ProfileRepository profileRepository})
      : _profileRepository = profileRepository,
        super(ProfileInitial()) {
    on<FetchUserDetailsEvent>(_onFetchUserDetails);
  }

  Future<void> _onFetchUserDetails(
    FetchUserDetailsEvent event,
    Emitter<ProfileState> emit,
  ) async {
    emit(ProfileLoading());
    try {
      final userDetails = await _profileRepository.fetchUserDetails();
      emit(ProfileLoaded(userDetails: userDetails));
    } catch (e) {
      emit(ProfileError(message: e.toString()));
    }
  }
}
