package com.example.demo.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云COS配置类
 */
@Configuration
public class CosConfig {

    @Value("${tencent.cos.secret-id:}")
    private String secretId;

    @Value("${tencent.cos.secret-key:}")
    private String secretKey;

    @Value("${tencent.cos.region:ap-guangzhou}")
    private String region;

    @Value("${tencent.cos.bucket-name:}")
    private String bucketName;

    @Value("${tencent.cos.enabled:false}")
    private boolean enabled;

    @Value("${tencent.cos.prefix:}")
    private String prefix;

    @Bean
    public COSClient cosClient() {
        if (!enabled || secretId.isEmpty() || secretKey.isEmpty()) {
            // 如果未配置COS，返回null（服务层需要处理这种情况）
            return null;
        }

        // 1 初始化用户身份信息（secretId, secretKey）
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);

        // 2 设置bucket的区域
        Region regionObj = new Region(region);
        ClientConfig clientConfig = new ClientConfig(regionObj);

        // 3 生成cos客户端
        return new COSClient(cred, clientConfig);
    }

    public String getBucketName() {
        return bucketName;
    }

    public String getRegion() {
        return region;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public String getPrefix() {
        return prefix;
    }
}
