package com.dora.util;

import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

/**
 * OSS工具类
 */
@Slf4j
public class OssUtil {

    /**
     * 生成唯一的文件名
     *
     * @param originalFileName 原始文件名
     * @param userId          用户ID
     * @return 唯一文件名
     */
    public static String generateUniqueFileName(String originalFileName, Long userId) {
        String extension = "";
        if (originalFileName != null && originalFileName.contains(".")) {
            extension = originalFileName.substring(originalFileName.lastIndexOf("."));
        }
        
        String timestamp = String.valueOf(System.currentTimeMillis());
        String random = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        
        return userId + "_" + timestamp + "_" + random + extension;
    }

    /**
     * 验证文件扩展名是否允许
     *
     * @param fileName 文件名
     * @return 是否允许
     */
    public static boolean isAllowedFileType(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return false;
        }
        
        String extension = "";
        if (fileName.contains(".")) {
            extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        }
        
        // 允许的文件格式：图片 + 压缩包
        String[] allowedExtensions = {
            // 图片格式
            ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp",
            // 压缩包格式
            ".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz",
            // 其他常用格式
            ".pdf", ".txt", ".md", ".json", ".xml", ".csv"
        };
        
        for (String allowedExt : allowedExtensions) {
            if (allowedExt.equals(extension)) {
                return true;
            }
        }
        
        return false;
    }

    /**
     * 获取文件大小限制（字节）
     *
     * @return 文件大小限制
     */
    public static long getMaxFileSize() {
        // 10MB
        return 10 * 1024 * 1024;
    }

    /**
     * 验证文件大小是否在限制范围内
     *
     * @param fileSize 文件大小（字节）
     * @return 是否在限制范围内
     */
    public static boolean isFileSizeValid(long fileSize) {
        return fileSize > 0 && fileSize <= getMaxFileSize();
    }

    /**
     * 格式化文件大小
     *
     * @param bytes 字节数
     * @return 格式化后的文件大小
     */
    public static String formatFileSize(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.2f KB", bytes / 1024.0);
        } else if (bytes < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", bytes / (1024.0 * 1024.0));
        } else {
            return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0));
        }
    }

    /**
     * 根据文件扩展名获取Content-Type
     *
     * @param fileName 文件名
     * @return Content-Type
     */
    public static String getContentType(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return "application/octet-stream";
        }
        
        String extension = "";
        if (fileName.contains(".")) {
            extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        }
        
        switch (extension) {
            // 图片格式
            case ".jpg":
            case ".jpeg":
                return "image/jpeg";
            case ".png":
                return "image/png";
            case ".gif":
                return "image/gif";
            case ".bmp":
                return "image/bmp";
            case ".webp":
                return "image/webp";
            // 压缩包格式
            case ".zip":
                return "application/zip";
            case ".rar":
                return "application/vnd.rar";
            case ".7z":
                return "application/x-7z-compressed";
            case ".tar":
                return "application/x-tar";
            case ".gz":
                return "application/gzip";
            case ".bz2":
                return "application/x-bzip2";
            case ".xz":
                return "application/x-xz";
            // 文档格式
            case ".pdf":
                return "application/pdf";
            case ".txt":
                return "text/plain";
            case ".md":
                return "text/markdown";
            case ".json":
                return "application/json";
            case ".xml":
                return "application/xml";
            case ".csv":
                return "text/csv";
            default:
                return "application/octet-stream";
        }
    }
} 