package com.agent.management.rag.kb;

import com.agent.management.model.entity.RagKnowledgeSourceBinding;
import com.agent.management.rag.bridge.RagAiBridgeClient;
import com.agent.management.rag.model.RagSourceType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class RagQuestionPlanningService {
    private final RagAiBridgeClient bridge;

    public Map<RagSourceType,String> plan(String question, List<RagKnowledgeSourceBinding> bindings) {
        Map<String,String> capabilities = new LinkedHashMap<>();
        bindings.forEach(binding -> capabilities.put(binding.getSourceType().name(),
                binding.getConfigJson() == null ? "enabled source" : binding.getConfigJson()));
        try {
            Map<String,Object> response = bridge.plan(Map.of("question",question,"capabilities",capabilities));
            Map<RagSourceType,String> plan = new EnumMap<>(RagSourceType.class);
            response.forEach((key,value) -> {
                try { if(value!=null&&!String.valueOf(value).isBlank())plan.put(RagSourceType.valueOf(key),String.valueOf(value)); }
                catch (IllegalArgumentException ignored) {}
            });
            return plan;
        } catch (Exception ignored) {
            return Map.of();
        }
    }
}
