package com.emoon.knowledge.chain.loader;

import com.emoon.knowledge.chain.split.TextSplitter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Word 文档加载器
 * 对应原 org.ruoyi.chain.loader.WordLoader 的能力迁移
 */
@Component
@AllArgsConstructor
@Slf4j
public class WordLoader implements ResourceLoader {

    private final TextSplitter textSplitter;

    @Override
    public String getContent(InputStream inputStream) {
        try (XWPFDocument document = new XWPFDocument(inputStream);
             XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
            return extractor.getText();
        } catch (IOException e) {
            log.error("读取 Word 文件失败", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> getChunkList(String content, String kid) {
        return textSplitter.split(content, kid);
    }
}
