package com.zhentao.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MinIO配置类
 */
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioConfig {

    /**
     * MinIO服务地址
     */
    private String endpoint;

    /**
     * 访问密钥
     */
    private String accessKey;

    /**
     * 秘密密钥
     */
    private String secretKey;

    /**
     * 存储桶名称
     */
    private String bucketName;

    /**
     * 成功凭证存储桶名称
     */
    private String proofBucketName = "Proof-of-success";

    /**
     * 创建MinioClient Bean
     */
    @Bean
    public MinioClient minioClient() {
        if (endpoint == null || endpoint.isEmpty()) {
            throw new IllegalStateException("MinIO endpoint 未配置！请检查 application.yml 中的 minio.endpoint 配置");
        }
        if (accessKey == null || accessKey.isEmpty()) {
            throw new IllegalStateException("MinIO accessKey 未配置！请检查 application.yml 中的 minio.access-key 配置");
        }
        if (secretKey == null || secretKey.isEmpty()) {
            throw new IllegalStateException("MinIO secretKey 未配置！请检查 application.yml 中的 minio.secret-key 配置");
        }
        
        System.out.println("✅ MinIO 配置加载成功:");
        System.out.println("   - endpoint: " + endpoint);
        System.out.println("   - accessKey: " + accessKey);
        System.out.println("   - bucketName: " + bucketName);
        
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}
