package com.airlock.iam.servicecontainer.app.infrastructure.event.azure;

import com.airlock.iam.servicecontainer.app.domain.service.event.EventHubBatch;
import com.airlock.iam.servicecontainer.app.domain.service.event.EventHubClient;
import com.azure.messaging.eventhubs.EventHubProducerClient;
import com.azure.messaging.eventhubs.models.CreateBatchOptions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;

public final class AzureEventHubClient implements EventHubClient {
   private final EventHubProducerClient client;
   @Nullable
   private final String partitionKey;

   @Override
   public EventHubBatch createBatch() {
      CreateBatchOptions options = new CreateBatchOptions();
      if (this.partitionKey != null) {
         options.setPartitionKey(this.partitionKey);
      }

      return new AzureEventHubBatch(this.client.createBatch(options));
   }

   @Override
   public void send(EventHubBatch batch) {
      this.client.send(((AzureEventHubBatch)batch).getBatch());
   }

   @Override
   public void testConnection() {
      this.client.getEventHubProperties();
   }

   @Override
   public void close() {
      this.client.close();
   }

   @SuppressFBWarnings(justification = "generated code")
   public AzureEventHubClient(EventHubProducerClient client, @Nullable String partitionKey) {
      this.client = client;
      this.partitionKey = partitionKey;
   }
}
