import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import '../../repository/recent_scan_history_repository.dart';
import '../../models/recent_scan_history_model.dart';

part 'recent_scan_history_event.dart';
part 'recent_scan_history_state.dart';

class RecentScanHistoryBloc extends Bloc<RecentScanHistoryEvent, RecentScanHistoryState> {
  final RecentScanHistoryRepository _repository;

  RecentScanHistoryBloc(this._repository) : super(RecentScanHistoryInitial()) {
    on<FetchRecentScanHistory>(_onFetchRecentScanHistory);
  }

  Future<void> _onFetchRecentScanHistory(
      FetchRecentScanHistory event, Emitter<RecentScanHistoryState> emit) async {
    emit(RecentScanHistoryLoading());
    try {
      final history = await _repository.fetchRecentScanHistory();
      emit(RecentScanHistoryLoaded(history));
    } catch (e) {
      emit(RecentScanHistoryError(e.toString()));
    }
  }
}
