package com.njcn.gather.aireport.reportmodel.component;

import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.gather.aireport.common.constant.AiReportFileConst;
import com.njcn.gather.aireport.common.constant.AiReportFileExtensionConst;
import com.njcn.gather.aireport.common.storage.AiReportMinioStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.util.Locale;

/**
 * 报告模板文件 MinIO 存储服务。
 */
@Component
@RequiredArgsConstructor
public class ReportModelFileStorageService {

    private final AiReportMinioStorageService minioStorageService;

    public StoredFile save(MultipartFile templateFile) {
        validateTemplateFile(templateFile);
        AiReportMinioStorageService.StoredFile storedFile = minioStorageService.saveMultipartFile(
                AiReportFileConst.BIZ_DIR_REPORT_MODEL, null, templateFile, false);
        return new StoredFile(storedFile.getObjectName(), storedFile.getOriginalFileName());
    }

    public AiReportMinioStorageService.StoredObject getObject(String storedPath) {
        return minioStorageService.getObject(storedPath);
    }

    public byte[] readBytes(String storedPath) {
        return minioStorageService.readBytes(storedPath);
    }

    public Path downloadToTempFile(String storedPath, String fileName) {
        return minioStorageService.downloadToTempFile(storedPath, fileName);
    }

    public void deleteQuietly(String storedPath) {
        minioStorageService.deleteQuietly(storedPath);
    }

    public void deleteTempFileQuietly(Path tempFile) {
        minioStorageService.deleteTempFileQuietly(tempFile);
    }

    public String resolveDownloadFileName(String storedPath) {
        if (StrUtil.isBlank(storedPath)) {
            return "report-model.docx";
        }
        int slashIndex = storedPath.lastIndexOf('/');
        String fileName = slashIndex >= 0 ? storedPath.substring(slashIndex + 1) : storedPath;
        int separatorIndex = fileName.indexOf('-');
        return separatorIndex >= 0 && separatorIndex + 1 < fileName.length() ? fileName.substring(separatorIndex + 1) : fileName;
    }

    private void validateTemplateFile(MultipartFile templateFile) {
        if (templateFile == null || templateFile.isEmpty()) {
            throw new IllegalArgumentException("模板文件不能为空");
        }
        String fileName = templateFile.getOriginalFilename();
        if (StrUtil.isBlank(fileName)) {
            throw new IllegalArgumentException("模板文件名不能为空");
        }
        String extension = resolveExtension(fileName);
        if (!AiReportFileExtensionConst.WORD_EXTENSIONS.contains(extension)) {
            throw new IllegalArgumentException("模板文件仅支持.docx格式");
        }
    }

    private String resolveExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex < 0 || dotIndex == fileName.length() - 1) {
            return "";
        }
        return fileName.substring(dotIndex + 1).toLowerCase(Locale.ENGLISH);
    }

    public static class StoredFile {
        private final String path;
        private final String fileName;

        public StoredFile(String path, String fileName) {
            this.path = path;
            this.fileName = fileName;
        }

        public String getPath() {
            return path;
        }

        public String getFileName() {
            return fileName;
        }
    }
}
