package cn.novalon.gym.manage.groupcourse.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 阿里云OSS工具类
 */
public class OSSUtil {

    private static final Logger logger = LoggerFactory.getLogger(OSSUtil.class);

    // OSS配置信息
    private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    private static final String ACCESS_KEY_ID = "LTAI5t9TFh9Vayeahz45kZjg";
    private static final String ACCESS_KEY_SECRET = "zD6NlCeH5UhjBs4vnQVqn8Ksi3CaZz";
    private static final String BUCKET_NAME = "ycc-filesaver";

    // OSS访问地址前缀
    private static final String OSS_URL_PREFIX = "https://" + BUCKET_NAME + "." + ENDPOINT + "/";

    // 文件存储目录
    private static final String QRCODE_DIR = "qrcode/";

    /**
     * 上传文件到阿里云OSS
     *
     * @param localFilePath 本地文件路径
     * @param fileName 文件名（不含路径）
     * @return OSS访问地址
     */
    public static String uploadToOSS(String localFilePath, String fileName) {
        OSS ossClient = null;
        try {
            // 创建OSS客户端
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            // 构建OSS文件路径：qrcode/2026/06/18/xxx.png
            String datePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            String ossFilePath = QRCODE_DIR + datePath + "/" + fileName;

            // 创建上传请求
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, ossFilePath, new File(localFilePath));

            // 上传文件
            ossClient.putObject(putObjectRequest);

            // 构建访问地址
            String accessUrl = OSS_URL_PREFIX + ossFilePath;

            logger.info("文件上传到OSS成功: localPath={}, ossUrl={}", localFilePath, accessUrl);

            return accessUrl;
        } catch (Exception e) {
            logger.error("文件上传到OSS失败 - localPath: {}, error: {}", localFilePath, e.getMessage(), e);
            throw new RuntimeException("文件上传到OSS失败: " + e.getMessage(), e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 上传文件到阿里云OSS（自定义存储路径）
     *
     * @param localFilePath 本地文件路径
     * @param ossDirectory OSS存储目录
     * @param fileName 文件名（不含路径）
     * @return OSS访问地址
     */
    public static String uploadToOSS(String localFilePath, String ossDirectory, String fileName) {
        OSS ossClient = null;
        try {
            // 创建OSS客户端
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

            // 构建OSS文件路径
            String ossFilePath = ossDirectory + fileName;

            // 创建上传请求
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, ossFilePath, new File(localFilePath));

            // 上传文件
            ossClient.putObject(putObjectRequest);

            // 构建访问地址
            String accessUrl = OSS_URL_PREFIX + ossFilePath;

            logger.info("文件上传到OSS成功: localPath={}, ossUrl={}", localFilePath, accessUrl);

            return accessUrl;
        } catch (Exception e) {
            logger.error("文件上传到OSS失败 - localPath: {}, error: {}", localFilePath, e.getMessage(), e);
            throw new RuntimeException("文件上传到OSS失败: " + e.getMessage(), e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}