import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow_core/core.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../../domain/usecases/get_certificates_usecase.dart';
import 'certificates_state.dart';

class CertificatesCubit extends Cubit<CertificatesState>
    with BlocErrorHandler<CertificatesState> {
  final GetCertificatesUseCase _getCertificatesUseCase;

  CertificatesCubit(this._getCertificatesUseCase)
    : super(const CertificatesState()) {
    loadCertificates();
  }

  Future<void> loadCertificates() async {
    emit(state.copyWith(status: CertificatesStatus.loading));
    await handleError(
      emit: emit,
      action: () async {
        final List<StaffDocument> certificates =
            await _getCertificatesUseCase();
        emit(
          state.copyWith(
            status: CertificatesStatus.success,
            certificates: certificates,
          ),
        );
      },
      onError:
          (String errorKey) => state.copyWith(
            status: CertificatesStatus.failure,
            errorMessage: errorKey,
          ),
    );
  }
}

