package tech.easyflow.ai.document.support;

import com.easyagents.document.core.entity.ParseFile;
import com.easyagents.document.core.entity.ParseRequest;
import com.easyagents.document.core.entity.PdfParseRequest;
import com.easyagents.document.core.entity.PptxParseRequest;
import com.easyagents.document.core.entity.XlsxParseRequest;
import org.springframework.stereotype.Component;
import tech.easyflow.ai.document.exception.DocumentParseBridgeException;
import tech.easyflow.ai.document.model.DocumentParseScenario;

/**
 * easy-agents 解析请求工厂。
 *
 * <p>负责把 easyflow 业务场景预设映射为底层统一解析请求。</p>
 *
 * @author Codex
 * @since 2026-04-14
 */
@Component
public class DocumentParseRequestFactory {

    /**
     * 构建统一解析请求。
     *
     * @param source 已加载文档源
     * @param scenario 解析场景
     * @return 统一解析请求
     */
    public ParseRequest build(LoadedDocumentSource source, DocumentParseScenario scenario) {
        if (source == null || source.getContentBytes() == null || source.getContentBytes().length == 0) {
            throw DocumentParseBridgeException.requestBuildFailed("文档源内容为空，无法构建解析请求");
        }
        if (scenario == null) {
            throw DocumentParseBridgeException.requestBuildFailed("解析场景不能为空");
        }
        ParseRequest request = createTypedRequest(source);
        request.addFile(ParseFile.of(source.getFileName(), source.getContentBytes(), source.getContentType()));
        applyScenario(request, scenario);
        return request;
    }

    private ParseRequest createTypedRequest(LoadedDocumentSource source) {
        DocumentParseSourceType sourceType = DocumentParseSourceType.resolve(source.getFileName(), source.getContentType());
        switch (sourceType) {
            case PDF:
                return new PdfParseRequest();
            case PPTX:
                return new PptxParseRequest();
            case XLSX:
                return new XlsxParseRequest();
            case DOCX:
                return new ParseRequest();
            default:
                throw DocumentParseBridgeException.requestBuildFailed("当前文件类型暂不支持桥接解析");
        }
    }

    private void applyScenario(ParseRequest request, DocumentParseScenario scenario) {
        switch (scenario) {
            case WORKFLOW_TEXT:
                request.setReturnMarkdown(Boolean.TRUE);
                request.setReturnMiddleJson(Boolean.FALSE);
                request.setReturnContentList(Boolean.FALSE);
                request.setReturnModelOutput(Boolean.FALSE);
                request.setReturnImages(Boolean.FALSE);
                break;
            case KNOWLEDGE_IMPORT:
                request.setReturnMarkdown(Boolean.TRUE);
                request.setReturnMiddleJson(Boolean.TRUE);
                request.setReturnContentList(Boolean.TRUE);
                request.setReturnModelOutput(Boolean.FALSE);
                request.setReturnImages(Boolean.TRUE);
                break;
            case FULL_ARTIFACTS:
                request.setReturnMarkdown(Boolean.TRUE);
                request.setReturnMiddleJson(Boolean.TRUE);
                request.setReturnContentList(Boolean.TRUE);
                request.setReturnModelOutput(Boolean.TRUE);
                request.setReturnImages(Boolean.TRUE);
                break;
            default:
                throw DocumentParseBridgeException.requestBuildFailed("不支持的文档解析场景: " + scenario);
        }
    }
}
