package com.octal.connector.kinesis

import com.sksamuel.tabby.option.Option
import com.sksamuel.tabby.option.mapN
import dev.clunky.domain.ClusterConnectionConfig
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.kinesis.KinesisAsyncClient

abstract class KinesisOps(config: ClusterConnectionConfig.Kinesis) {

   private val providers = listOfNotNull(
      Option.mapN(
         config.accessKeyId,
         config.secretAccessKey
      ) { accessKeyId, secretAccessKey ->
         StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey))
      }.orNull(),
      DefaultCredentialsProvider.create(),
   ).toTypedArray()

   private val creds = AwsCredentialsProviderChain.of(*providers)

   protected val client: KinesisAsyncClient = KinesisAsyncClient.builder()
      .region(Region.US_EAST_1)
      .credentialsProvider(creds)
      .build()

}
