import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../model/support_details_model.dart';
import '../../repository/support_details_repository.dart';

part 'support_details_event.dart';
part 'support_details_state.dart';

class SupportDetailsBloc extends Bloc<SupportDetailsEvent, SupportDetailsState> {
  final SupportDetailsRepository _repository;

  SupportDetailsBloc({SupportDetailsRepository? repository})
      : _repository = repository ?? SupportDetailsRepository(),
        super(const SupportDetailsInitial()) {
    on<FetchSupportDetailsEvent>(_onFetchSupportDetails);
  }

  Future<void> _onFetchSupportDetails(
      FetchSupportDetailsEvent event,
      Emitter<SupportDetailsState> emit,
      ) async {
    emit(const SupportDetailsLoading());
    try {
      final SupportDetailModel data = await _repository.fetchSupportDetails();
      emit(SupportDetailsLoaded(supportDetail: data));
    } catch (e) {
      emit(SupportDetailsError(message: e.toString()));
    }
  }
}