package com.caliverse.admin.global.configuration;

import com.caliverse.admin.domain.entity.CLOTHSMALLTYPE;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Configuration
public class DynamoDBConfig {
    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

    @Value("${amazon.aws.region}")
    private String amazonAWSRegion;

    @Bean
    public DynamoDbClient dynamoDbClient() {
        var builder = DynamoDbClient.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(awsCredentialsProvider());

        if (amazonDynamoDBEndpoint != null && !amazonDynamoDBEndpoint.isEmpty()) {
            builder.endpointOverride(java.net.URI.create(amazonDynamoDBEndpoint));
        }
        return builder.build();
    }

    @Bean
    public AwsCredentialsProvider awsCredentialsProvider() {
        if (amazonAWSAccessKey == null || amazonAWSAccessKey.isEmpty() ||amazonAWSSecretKey == null || amazonAWSSecretKey.isEmpty()) {
            return StaticCredentialsProvider.create(AwsBasicCredentials.create("fakeAccesskey", "fakeSecretKey"));
        }
        return StaticCredentialsProvider.create(AwsBasicCredentials.create(amazonAWSAccessKey, amazonAWSSecretKey));
    }

    @Bean
    public CloudWatchClient cloudWatchClient() {
        return CloudWatchClient.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(awsCredentialsProvider())
                .build();
    }

    @Bean
    public DynamoDbEnhancedClient dynamoDbEnhancedClient(DynamoDbClient dynamoDbClient) {
        return DynamoDbEnhancedClient.builder()
                .dynamoDbClient(dynamoDbClient)
                .build();
    }
}
