package xtools.boot.storage.s3.config;

import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import xtools.boot.storage.base.config.StorageConfig;
import xtools.boot.storage.base.service.StorageService;
import xtools.boot.storage.s3.service.impl.StorageServiceS3Impl;
import xtools.extend.S3Utils;

/**
 * <p>Title : S3StorageConfig</p>
 * <p>Description : S3StorageConfig</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/3/14 07:53
 */
@Configuration
public class S3StorageConfig {

    @Resource
    private StorageConfig storageConfig;

    /**
     * 创建S3默认客户端
     *
     * @return S3Client
     */
    @Bean
    @ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
    @ConditionalOnProperty(prefix = "storage.s3", name = "server", havingValue = "default")
    public S3Client defaultClient() {
        return S3Utils.client(
                storageConfig.getS3().getHost(),
                storageConfig.getS3().getAccessKeyId(),
                storageConfig.getS3().getSecretAccessKey(),
                Region.CN_NORTH_1,
                true
        );
    }

    /**
     * 创建S3-rustfs客户端
     *
     * @return S3Client
     */
    @Bean
    @ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
    @ConditionalOnProperty(prefix = "storage.s3", name = "server", havingValue = "rustfs")
    public S3Client rustfsClient() {
        return S3Utils.rustfsClient(
                storageConfig.getS3().getHost(),
                storageConfig.getS3().getAccessKeyId(),
                storageConfig.getS3().getSecretAccessKey()
        );
    }

    /**
     * 创建S3存储服务
     *
     * @return StorageService
     */
    @Bean
    @ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
    public StorageService s3Storage() {
        return new StorageServiceS3Impl();
    }
}
