package at.procon.dip.search.integration;

import at.procon.dip.domain.document.DocumentFamily;
import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.RepresentationType;
import at.procon.dip.search.dto.SearchMode;
import at.procon.dip.search.dto.SearchRepresentationSelectionMode;
import at.procon.dip.search.dto.SearchRequest;
import at.procon.dip.search.dto.SearchResponse;
import at.procon.dip.search.service.SearchOrchestrator;
import at.procon.dip.search.spi.SearchDocumentScope;
import at.procon.dip.testsupport.AbstractSemanticSearchIntegrationTest;
import at.procon.dip.testsupport.SemanticSearchTestDataFactory;
import java.util.Set;

import at.procon.dip.testsupport.config.SearchTestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

import static org.assertj.core.api.Assertions.assertThat;

@Import(SearchTestConfig.class)
class SemanticModelSelectionIntegrationTest extends AbstractSemanticSearchIntegrationTest {

    @Autowired
    private SemanticSearchTestDataFactory dataFactory;

    @Autowired
    private SearchOrchestrator searchOrchestrator;

    @Test
    void semanticModelKey_should_control_which_embeddings_are_used() {
        dataFactory.createAndEmbedPrimaryRepresentation(
                "Heat network planning",
                "Alt-model semantic document",
                "District heating optimization strategy for municipal networks.",
                DocumentType.TEXT,
                DocumentFamily.GENERIC,
                "en",
                RepresentationType.SEMANTIC_TEXT,
                "mock-search-alt"
        );

        SearchRequest defaultModelRequest = SearchRequest.builder()
                .queryText("district heating optimization")
                .modes(Set.of(SearchMode.SEMANTIC))
                .representationSelectionMode(SearchRepresentationSelectionMode.PRIMARY_ONLY)
                .build();

        SearchRequest alternateModelRequest = SearchRequest.builder()
                .queryText("district heating optimization")
                .modes(Set.of(SearchMode.SEMANTIC))
                .representationSelectionMode(SearchRepresentationSelectionMode.PRIMARY_ONLY)
                .semanticModelKey("mock-search-alt")
                .build();

        SearchResponse defaultModelResponse = searchOrchestrator.search(
                defaultModelRequest,
                new SearchDocumentScope(Set.of(), Set.of(DocumentType.TEXT), Set.of(DocumentFamily.GENERIC), null, null, null)
        );

        SearchResponse alternateModelResponse = searchOrchestrator.search(
                alternateModelRequest,
                new SearchDocumentScope(Set.of(), Set.of(DocumentType.TEXT), Set.of(DocumentFamily.GENERIC), null, null, null)
        );

        assertThat(defaultModelResponse.getHits()).isEmpty();
        assertThat(alternateModelResponse.getHits()).hasSize(1);
        assertThat(alternateModelResponse.getHits().getFirst().getTitle()).isEqualTo("Heat network planning");
    }
}
