import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow_core/core.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../../domain/usecases/get_documents_usecase.dart';
import 'documents_state.dart';

class DocumentsCubit extends Cubit<DocumentsState>
    with BlocErrorHandler<DocumentsState> {
  final GetDocumentsUseCase _getDocumentsUseCase;

  DocumentsCubit(this._getDocumentsUseCase) : super(const DocumentsState());

  Future<void> loadDocuments() async {
    emit(state.copyWith(status: DocumentsStatus.loading));
    await handleError(
      emit: emit,
      action: () async {
        final List<StaffDocument> documents = await _getDocumentsUseCase();
        emit(
          state.copyWith(
            status: DocumentsStatus.success,
            documents: documents,
          ),
        );
      },
      onError:
          (String errorKey) => state.copyWith(
            status: DocumentsStatus.failure,
            errorMessage: errorKey,
          ),
    );
  }
}

