import 'package:flutter_bloc/flutter_bloc.dart';
import '../../repositories/scan_history_repository.dart';
import '../../models/scan_history_details_model.dart';
import 'package:equatable/equatable.dart';
part 'scan_history_details_event.dart';
part 'scan_history_details_state.dart';

class ScanHistoryDetailsBloc extends Bloc<ScanHistoryDetailsEvent, ScanHistoryDetailsState> {
  final ScanHistoryRepository _repository;

  ScanHistoryDetailsBloc(this._repository) : super(ScanHistoryDetailsInitial()) {
    on<FetchScanHistoryDetails>(_onFetchScanHistoryDetails);
  }

  Future<void> _onFetchScanHistoryDetails(
      FetchScanHistoryDetails event, Emitter<ScanHistoryDetailsState> emit) async {
    emit(ScanHistoryDetailsLoading());
    try {
      final details = await _repository.fetchScanHistoryDetails(event.id);
      emit(ScanHistoryDetailsLoaded(details));
    } catch (e) {
      emit(ScanHistoryDetailsError(e.toString()));
    }
  }
}
