package tech.easyflow.ai.node.filegeneration;

import org.springframework.util.StringUtils;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.util.Arrays;

/**
 * 文件导出的目标格式。
 *
 * @author Codex
 * @since 2026-04-18
 */
public enum TargetFormat {
    MD("md", "text/markdown"),
    HTML("html", "text/html"),
    PDF("pdf", "application/pdf"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

    private final String value;
    private final String contentType;

    TargetFormat(String value, String contentType) {
        this.value = value;
        this.contentType = contentType;
    }

    public String getValue() {
        return value;
    }

    public String getContentType() {
        return contentType;
    }

    /**
     * 解析目标格式。
     *
     * @param value 原始值
     * @return 目标格式枚举
     */
    public static TargetFormat fromValue(String value) {
        if (!StringUtils.hasText(value)) {
            throw new BusinessException("文件导出目标格式不能为空");
        }
        String normalized = value.trim().toLowerCase();
        return Arrays.stream(values())
                .filter(item -> item.value.equals(normalized))
                .findFirst()
                .orElseThrow(() -> new BusinessException("暂不支持的文件导出目标格式: " + value));
    }
}
