@Override
  public Mono<GetPreKeyCountResponse> getPreKeyCount(final GetPreKeyCountRequest request) {
    return Mono.fromSupplier(AuthenticationUtil::requireAuthenticatedDevice)
        .flatMap(authenticatedDevice -> Mono.fromFuture(() -> accountsManager.getByAccountIdentifierAsync(authenticatedDevice.accountIdentifier()))
            .map(maybeAccount -> maybeAccount
                .map(account -> Tuples.of(account, authenticatedDevice.deviceId()))
                .orElseThrow(Status.UNAUTHENTICATED::asRuntimeException)))
        .flatMapMany(accountAndDeviceId -> Flux.just(
            Tuples.of(IdentityType.ACI, accountAndDeviceId.getT1().getUuid(), accountAndDeviceId.getT2()),
            Tuples.of(IdentityType.PNI, accountAndDeviceId.getT1().getPhoneNumberIdentifier(), accountAndDeviceId.getT2())
        ))
        .flatMap(identityTypeUuidAndDeviceId -> Flux.merge(
            Mono.fromFuture(() -> keysManager.getEcCount(identityTypeUuidAndDeviceId.getT2(), identityTypeUuidAndDeviceId.getT3()))
                .map(ecKeyCount -> Tuples.of(identityTypeUuidAndDeviceId.getT1(), PreKeyType.EC, ecKeyCount)),

            Mono.fromFuture(() -> keysManager.getPqCount(identityTypeUuidAndDeviceId.getT2(), identityTypeUuidAndDeviceId.getT3()))
                .map(ecKeyCount -> Tuples.of(identityTypeUuidAndDeviceId.getT1(), PreKeyType.KEM, ecKeyCount))
        ))
        .reduce(GetPreKeyCountResponse.newBuilder(), (builder, tuple) -> {
          final IdentityType identityType = tuple.getT1();
          final PreKeyType preKeyType = tuple.getT2();
          final int count = tuple.getT3();

          switch (identityType) {
            case ACI -> {
              switch (preKeyType) {
                case EC -> builder.setAciEcPreKeyCount(count);
                case KEM -> builder.setAciKemPreKeyCount(count);
              }
            }
            case PNI -> {
              switch (preKeyType) {
                case EC -> builder.setPniEcPreKeyCount(count);
                case KEM -> builder.setPniKemPreKeyCount(count);
              }
            }
          }

          return builder;
        })
        .map(GetPreKeyCountResponse.Builder::build);
  }
@Test
  void getPreKeyCount() {
    when(keysManager.getEcCount(AUTHENTICATED_ACI, AUTHENTICATED_DEVICE_ID))
        .thenReturn(CompletableFuture.completedFuture(1));

    when(keysManager.getPqCount(AUTHENTICATED_ACI, AUTHENTICATED_DEVICE_ID))
        .thenReturn(CompletableFuture.completedFuture(2));

    when(keysManager.getEcCount(AUTHENTICATED_PNI, AUTHENTICATED_DEVICE_ID))
        .thenReturn(CompletableFuture.completedFuture(3));

    when(keysManager.getPqCount(AUTHENTICATED_PNI, AUTHENTICATED_DEVICE_ID))
        .thenReturn(CompletableFuture.completedFuture(4));

    assertEquals(GetPreKeyCountResponse.newBuilder()
            .setAciEcPreKeyCount(1)
            .setAciKemPreKeyCount(2)
            .setPniEcPreKeyCount(3)
            .setPniKemPreKeyCount(4)
            .build(),
        authenticatedServiceStub().getPreKeyCount(GetPreKeyCountRequest.newBuilder().build()));
  }