package com.agent.management.rag.memory;

import com.agent.management.rag.capability.RagCapabilityProfileService;
import com.agent.management.rag.graph.GraphSchemaValidator;
import com.agent.management.rag.model.RagSourceType;
import com.agent.management.service.GraphSourceService;
import com.agent.management.service.SchemaExplorerService;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

class RagExampleCandidateServiceTest {
    @Test
    void importsOnlyCandidatesThatPassDatabaseExplain() {
        RagExampleMemoryService memory = mock(RagExampleMemoryService.class);
        SchemaExplorerService sql = mock(SchemaExplorerService.class);
        doThrow(new IllegalArgumentException("unknown column")).when(sql).explainQuery(1L, "SELECT bad FROM missing");
        when(memory.list(RagSourceType.STRUCTURED_DATA, "1")).thenReturn(List.of());
        RagExampleCandidateService service = new RagExampleCandidateService(memory, mock(RagCapabilityProfileService.class),
                mock(GraphSchemaValidator.class), mock(GraphSourceService.class), sql);

        Map<String,Object> result = service.importValidated(RagSourceType.STRUCTURED_DATA, "1", List.of(
                Map.of("question", "正常", "sql", "SELECT 1"),
                Map.of("question", "错误", "sql", "SELECT bad FROM missing")));

        assertThat(result).containsEntry("accepted", 1);
        assertThat((List<?>)result.get("rejected")).hasSize(1);
        verify(memory, times(1)).addCandidates(eq(RagSourceType.STRUCTURED_DATA), eq("1"), anyList());
    }
}
