package com.dora.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.common.utils.LogUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云OSS配置类
 */
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class OssConfig {

    /**
     * OSS端点
     */
    private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

    /**
     * 存储桶名称
     */
    private String bucketName = "oss-1818ai-user-img";

    /**
     * 地域
     */
    private String region = "cn-hangzhou";

    /**
     * 用户图片文件夹
     */
    private String userImgFolder = "user_img/";

    /**
     * 预签名URL过期时间（秒）
     */
    private Long expirationSeconds = 3600L;

    /**
     * AccessKey ID
     */
    private String accessKeyId;

    /**
     * AccessKey Secret
     */
    private String accessKeySecret;

    /**
     * 创建OSS客户端
     */
    @Bean
    public OSS ossClient() {
        try {
            // 使用配置文件中的访问凭证
            String accessKeyId = this.accessKeyId;
            String accessKeySecret = this.accessKeySecret;

            // 创建客户端配置
            com.aliyun.oss.ClientBuilderConfiguration clientConfig = 
                new com.aliyun.oss.ClientBuilderConfiguration();
            // 使用V2签名版本，避免region问题
            clientConfig.setSignatureVersion(SignVersion.V2);

            // 创建OSS客户端
            return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, clientConfig);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create OSS client", e);
        }
    }
} 