package tech.easyflow.ai.document.support;

import com.easyagents.document.core.entity.ParseArtifacts;
import com.easyagents.document.core.entity.ParseResult;
import com.easyagents.document.core.entity.ParseTaskInfo;
import com.easyagents.document.core.entity.ParseTaskStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import tech.easyflow.ai.document.model.DocumentParseArtifacts;
import tech.easyflow.ai.document.model.DocumentParseTaskInfo;
import tech.easyflow.ai.document.model.DocumentParseTaskStatus;
import tech.easyflow.ai.document.model.DocumentParsedResult;

/**
 * easy-agents 结果映射器。
 *
 * <p>负责把底层解析结果转换为 easyflow 侧稳定 DTO，并统一 preferredText 规则。</p>
 *
 * @author Codex
 * @since 2026-04-14
 */
@Component
public class DocumentParseResultMapper {

    /**
     * 映射单文件解析结果。
     *
     * @param parseResult 底层结果
     * @return easyflow 结果
     */
    public DocumentParsedResult map(ParseResult parseResult) {
        DocumentParsedResult document = new DocumentParsedResult();
        if (parseResult == null) {
            return document;
        }
        document.setFileName(parseResult.getFileName());
        document.setMarkdown(parseResult.getMarkdown());
        document.setPlainText(parseResult.getPlainText());
        document.setPreferredText(resolvePreferredText(parseResult));
        document.setPages(parseResult.getPages());
        document.setBlocks(parseResult.getBlocks());
        document.setTables(parseResult.getTables());
        document.setImages(parseResult.getImages());
        document.setWarnings(parseResult.getWarnings());
        document.setMetadata(parseResult.getMetadata());
        document.setArtifacts(mapArtifacts(parseResult.getArtifacts()));
        return document;
    }

    /**
     * 映射异步任务状态。
     *
     * @param taskStatus 底层任务状态
     * @return easyflow 任务状态
     */
    public DocumentParseTaskStatus map(ParseTaskStatus taskStatus) {
        DocumentParseTaskStatus status = new DocumentParseTaskStatus();
        if (taskStatus == null) {
            return status;
        }
        status.setTaskId(taskStatus.getTaskId());
        status.setStatus(taskStatus.getStatus());
        status.setBackend(taskStatus.getBackend());
        status.setFileNames(taskStatus.getFileNames());
        status.setCreatedAt(taskStatus.getCreatedAt());
        status.setStartedAt(taskStatus.getStartedAt());
        status.setCompletedAt(taskStatus.getCompletedAt());
        status.setError(taskStatus.getError());
        status.setStatusUrl(taskStatus.getStatusUrl());
        status.setResultUrl(taskStatus.getResultUrl());
        status.setQueuedAhead(taskStatus.getQueuedAhead());
        status.setProgressPercent(taskStatus.getProgressPercent());
        status.setCurrentStage(taskStatus.getCurrentStage());
        status.setProcessedItems(taskStatus.getProcessedItems());
        status.setTotalItems(taskStatus.getTotalItems());
        status.setStatusMessage(taskStatus.getStatusMessage());
        return status;
    }

    /**
     * 映射任务聚合查询结果。
     *
     * @param taskInfo 底层任务聚合结果
     * @return easyflow 聚合任务结果
     */
    public DocumentParseTaskInfo map(ParseTaskInfo taskInfo) {
        DocumentParseTaskInfo mapped = new DocumentParseTaskInfo();
        if (taskInfo == null) {
            return mapped;
        }
        fillTaskStatus(mapped, taskInfo);
        if (taskInfo.getResult() != null
            && taskInfo.getResult().getResults() != null
            && !taskInfo.getResult().getResults().isEmpty()) {
            mapped.setResult(map(taskInfo.getResult().getResults().get(0)));
        }
        return mapped;
    }

    private void fillTaskStatus(DocumentParseTaskStatus status, ParseTaskStatus taskStatus) {
        status.setTaskId(taskStatus.getTaskId());
        status.setStatus(taskStatus.getStatus());
        status.setBackend(taskStatus.getBackend());
        status.setFileNames(taskStatus.getFileNames());
        status.setCreatedAt(taskStatus.getCreatedAt());
        status.setStartedAt(taskStatus.getStartedAt());
        status.setCompletedAt(taskStatus.getCompletedAt());
        status.setError(taskStatus.getError());
        status.setStatusUrl(taskStatus.getStatusUrl());
        status.setResultUrl(taskStatus.getResultUrl());
        status.setQueuedAhead(taskStatus.getQueuedAhead());
        status.setProgressPercent(taskStatus.getProgressPercent());
        status.setCurrentStage(taskStatus.getCurrentStage());
        status.setProcessedItems(taskStatus.getProcessedItems());
        status.setTotalItems(taskStatus.getTotalItems());
        status.setStatusMessage(taskStatus.getStatusMessage());
    }

    private String resolvePreferredText(ParseResult parseResult) {
        if (StringUtils.hasText(parseResult.getMarkdown())) {
            return parseResult.getMarkdown();
        }
        return parseResult.getPlainText();
    }

    private DocumentParseArtifacts mapArtifacts(ParseArtifacts artifacts) {
        DocumentParseArtifacts mappedArtifacts = new DocumentParseArtifacts();
        if (artifacts == null) {
            return mappedArtifacts;
        }
        mappedArtifacts.setMiddleJson(artifacts.getMiddleJson());
        mappedArtifacts.setContentList(artifacts.getContentList());
        mappedArtifacts.setModelOutput(artifacts.getModelOutput());
        mappedArtifacts.setExtraJsonArtifacts(artifacts.getExtraJsonArtifacts());
        mappedArtifacts.setExtraBinaryArtifacts(artifacts.getExtraBinaryArtifacts());
        return mappedArtifacts;
    }
}
