package com.agent.management.rag.evaluation;

import com.agent.management.rag.kb.KnowledgeBaseRagRetriever;
import com.agent.management.rag.model.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.*;

@Service @RequiredArgsConstructor
public class RagEvaluationService {
    private final KnowledgeBaseRagRetriever retriever;
    public Map<String,Object> run(Long knowledgeBaseId, List<Map<String,Object>> cases) {
        List<Map<String,Object>> results=new ArrayList<>(); int passed=0; long totalMs=0;
        for(Map<String,Object> item:cases==null?List.<Map<String,Object>>of():cases){
            String question=String.valueOf(item.getOrDefault("question","")); String expected=String.valueOf(item.getOrDefault("expectedSource",""));
            KnowledgeBaseRagRequest request=new KnowledgeBaseRagRequest();request.setKnowledgeBaseId(knowledgeBaseId);request.setQuery(question);request.setTopK(5);
            if(!expected.isBlank()) request.setFilters(Map.of("sourceTypes",List.of(expected)));
            long start=System.currentTimeMillis(); KnowledgeBaseRagResult result=retriever.retrieve(request); long elapsed=System.currentTimeMillis()-start;totalMs+=elapsed;
            boolean sourceMatched=expected.isBlank()||result.getEvidences().stream().anyMatch(e->e.getSourceType().name().equalsIgnoreCase(expected));
            boolean ok=sourceMatched&&!result.getEvidences().isEmpty()&&result.getDiagnostics().isEmpty();if(ok)passed++;
            results.add(Map.of("question",question,"passed",ok,"evidenceCount",result.getEvidences().size(),"latencyMs",elapsed,"diagnostics",result.getDiagnostics()));
        }
        int total=results.size();return Map.of("total",total,"passed",passed,"passRate",total==0?0d:(double)passed/total,"averageLatencyMs",total==0?0:totalMs/total,"results",results);
    }
}
