package com.pavis.admin.aigc.core.llm.provider;

import com.pavis.admin.aigc.model.resp.KnowledgeResp;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.continew.starter.core.exception.BusinessException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author tycoding
 * @since 2024/3/8
 */
@Slf4j
@Component
@AllArgsConstructor
public class EmbeddingProvider {

    private final EmbeddingStoreFactory embeddingStoreFactory;
    private final KnowledgeStoreFactory knowledgeStoreFactory;
    private final ModelStoreFactory modelStoreFactory;

    public static DocumentSplitter splitter() {
        return DocumentSplitters.recursive(300, 20);
    }

    public EmbeddingModel getEmbeddingModel(List<Long> knowledgeIds) {
        List<String> storeIds = new ArrayList<>();
        knowledgeIds.forEach(id -> {
            if (knowledgeStoreFactory.containsKnowledge(id)) {
                KnowledgeResp data = knowledgeStoreFactory.getKnowledge(id);
                if (data.getEmbedModelId() != null) {
                    storeIds.add(data.getEmbedModelId().toString());
                }
            }
        });
        if (storeIds.isEmpty()) {
            throw new BusinessException("知识库缺少Embedding Model配置，请先检查配置");
        }

        HashSet<String> filterIds = new HashSet<>(storeIds);
        if (filterIds.size() > 1) {
            throw new BusinessException("存在多个不同Embedding Model的知识库，请先检查配置");
        }

        return modelStoreFactory.getEmbeddingModel(storeIds.get(0));
    }

    public EmbeddingModel getEmbeddingModel(Long knowledgeId) {
        if (knowledgeStoreFactory.containsKnowledge(knowledgeId)) {
            KnowledgeResp data = knowledgeStoreFactory.getKnowledge(knowledgeId);
            if (modelStoreFactory.containsEmbeddingModel(data.getEmbedModelId().toString())) {
                return modelStoreFactory.getEmbeddingModel(data.getEmbedModelId().toString());
            }
        }
        throw new BusinessException("没有找到匹配的Embedding向量数据库");
    }

    public EmbeddingStore<TextSegment> getEmbeddingStore(Long knowledgeId) {
        if (knowledgeStoreFactory.containsKnowledge(knowledgeId)) {
            KnowledgeResp data = knowledgeStoreFactory.getKnowledge(knowledgeId);
            if (embeddingStoreFactory.containsEmbeddingStore(data.getEmbedStoreId().toString())) {
                return embeddingStoreFactory.getEmbeddingStore(data.getEmbedStoreId().toString());
            }
        }
        throw new BusinessException("没有找到匹配的Embedding向量数据库");
    }

    public EmbeddingStore<TextSegment> getEmbeddingStore(List<Long> knowledgeIds) {
        List<String> storeIds = new ArrayList<>();
        knowledgeIds.forEach(id -> {
            if (knowledgeStoreFactory.containsKnowledge(id)) {
                KnowledgeResp data = knowledgeStoreFactory.getKnowledge(id);
                if (data.getEmbedStoreId() != null) {
                    storeIds.add(data.getEmbedStoreId().toString());
                }
            }
        });
        if (storeIds.isEmpty()) {
            throw new BusinessException("知识库缺少Embedding Store配置，请先检查配置");
        }

        HashSet<String> filterIds = new HashSet<>(storeIds);
        if (filterIds.size() > 1) {
            throw new BusinessException("存在多个不同Embedding Store数据源的知识库，请先检查配置");
        }

        return embeddingStoreFactory.getEmbeddingStore(storeIds.get(0));
    }

}
