package tech.easyflow.publicapi.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mybatisflex.core.paginate.Page;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.entity.Document;
import tech.easyflow.ai.entity.DocumentCollection;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

/**
 * {@link PublicKnowledgeDetailResponse} 响应结构测试。
 */
public class PublicKnowledgeDetailResponseTest {

    /**
     * 验证知识库字段保持在顶层，文档只暴露公开摘要。
     *
     */
    @Test
    public void shouldKeepKnowledgeFieldsAtTopLevelAndExposeDocumentSummary() {
        DocumentCollection knowledge = new DocumentCollection();
        knowledge.setId(BigInteger.valueOf(100));
        knowledge.setTitle("测试知识库");
        knowledge.setCollectionType(DocumentCollection.TYPE_DOCUMENT);

        Document document = new Document();
        document.setId(BigInteger.valueOf(200));
        document.setTitle("manual.pdf");
        document.setDocumentType("pdf");
        document.setContentType("application/pdf");
        document.setDocumentPath("private/path/manual.pdf");
        document.setContent("内部正文");
        document.setProcessStatus("INDEXED");
        document.setTotalChunks(12);
        document.setProgressPercent(100);
        Page<Document> source = new Page<>(List.of(document), 1, 10, 1L);

        PublicKnowledgeDetailResponse response =
            new PublicKnowledgeDetailResponse(knowledge, source);
        JSONObject json = JSON.parseObject(JSON.toJSONString(response));

        Assert.assertEquals("100", json.getString("id"));
        Assert.assertEquals("测试知识库", json.getString("title"));
        Assert.assertFalse(json.containsKey("knowledge"));
        JSONObject summary = json.getJSONObject("documents")
            .getJSONArray("records")
            .getJSONObject(0);
        Assert.assertEquals("manual.pdf", summary.getString("title"));
        Assert.assertEquals(12L, summary.getLongValue("chunkCount"));
        Assert.assertFalse(summary.containsKey("documentPath"));
        Assert.assertFalse(summary.containsKey("content"));
        Assert.assertFalse(summary.containsKey("options"));
    }

    /**
     * 验证空文档分页保留调用方请求的分页参数。
     */
    @Test
    public void shouldKeepRequestedPaginationForEmptyDocumentPage() {
        DocumentCollection knowledge = new DocumentCollection();
        knowledge.setId(BigInteger.valueOf(300));
        knowledge.setCollectionType(DocumentCollection.TYPE_FAQ);
        Page<Document> source = new Page<>(Collections.emptyList(), 3, 7, 0L);

        PublicKnowledgeDetailResponse response =
            new PublicKnowledgeDetailResponse(knowledge, source);

        Assert.assertEquals(3L, response.getDocuments().getPageNumber());
        Assert.assertEquals(7L, response.getDocuments().getPageSize());
        Assert.assertEquals(0L, response.getDocuments().getTotalRow());
        Assert.assertTrue(response.getDocuments().getRecords().isEmpty());
    }
}
