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.ParseResponse;
import com.easyagents.document.core.entity.ParseTaskInfo;
import com.easyagents.document.core.entity.ParseTaskStatus;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.document.model.DocumentParseTaskInfo;
import tech.easyflow.ai.document.model.DocumentParsedResult;

import java.util.Collections;

/**
 * {@link DocumentParseResultMapper} 单元测试。
 *
 * @author Codex
 * @since 2026-04-14
 */
public class DocumentParseResultMapperTest {

    /**
     * 验证 preferredText 按 markdown 优先、plainText 回退。
     */
    @Test
    public void shouldPreferMarkdown() {
        DocumentParseResultMapper mapper = new DocumentParseResultMapper();
        ParseResult result = new ParseResult();
        result.setFileName("demo.pdf");
        result.setMarkdown("# title");
        result.setPlainText("plain");

        DocumentParsedResult mapped = mapper.map(result);

        Assert.assertEquals("# title", mapped.getPreferredText());
    }

    /**
     * 验证结构化工件被正确映射。
     */
    @Test
    public void shouldMapArtifacts() {
        DocumentParseResultMapper mapper = new DocumentParseResultMapper();
        ParseResult result = new ParseResult();
        ParseArtifacts artifacts = new ParseArtifacts();
        artifacts.setMiddleJson(Collections.singletonMap("page", 1));
        artifacts.setContentList(Collections.singletonList("block"));
        artifacts.setModelOutput(Collections.singletonMap("raw", "ok"));
        result.setArtifacts(artifacts);

        DocumentParsedResult mapped = mapper.map(result);

        Assert.assertNotNull(mapped.getArtifacts());
        Assert.assertEquals(Collections.singletonMap("page", 1), mapped.getArtifacts().getMiddleJson());
        Assert.assertEquals(Collections.singletonList("block"), mapped.getArtifacts().getContentList());
        Assert.assertEquals(Collections.singletonMap("raw", "ok"), mapped.getArtifacts().getModelOutput());
    }

    /**
     * 验证任务聚合结果被正确映射。
     */
    @Test
    public void shouldMapTaskInfo() {
        DocumentParseResultMapper mapper = new DocumentParseResultMapper();
        ParseTaskInfo taskInfo = new ParseTaskInfo();
        taskInfo.setTaskId("task-1");
        taskInfo.setStatus("completed");
        taskInfo.setProgressPercent(100);
        taskInfo.setCurrentStage("completed");

        ParseResult result = new ParseResult();
        result.setFileName("demo.pdf");
        result.setMarkdown("# title");
        ParseResponse response = new ParseResponse();
        response.setResults(Collections.singletonList(result));
        taskInfo.setResult(response);

        DocumentParseTaskInfo mapped = mapper.map(taskInfo);

        Assert.assertEquals("task-1", mapped.getTaskId());
        Assert.assertEquals(Integer.valueOf(100), mapped.getProgressPercent());
        Assert.assertEquals("completed", mapped.getCurrentStage());
        Assert.assertNotNull(mapped.getResult());
        Assert.assertEquals("# title", mapped.getResult().getPreferredText());
    }

    /**
     * 验证异步进度字段被完整透传。
     */
    @Test
    public void shouldMapTaskStatusProgressFields() {
        DocumentParseResultMapper mapper = new DocumentParseResultMapper();
        ParseTaskStatus status = new ParseTaskStatus();
        status.setTaskId("task-2");
        status.setStatus("running");
        status.setProgressPercent(45);
        status.setCurrentStage("ocr");
        status.setProcessedItems(9);
        status.setTotalItems(20);
        status.setStatusMessage("正在识别图片");

        tech.easyflow.ai.document.model.DocumentParseTaskStatus mapped = mapper.map(status);

        Assert.assertEquals(Integer.valueOf(45), mapped.getProgressPercent());
        Assert.assertEquals("ocr", mapped.getCurrentStage());
        Assert.assertEquals(Integer.valueOf(9), mapped.getProcessedItems());
        Assert.assertEquals(Integer.valueOf(20), mapped.getTotalItems());
        Assert.assertEquals("正在识别图片", mapped.getStatusMessage());
    }
}
