package at.procon.dip.search.engine.trigram;

import at.procon.dip.search.api.SearchExecutionContext;
import at.procon.dip.search.dto.SearchEngineType;
import at.procon.dip.search.dto.SearchHit;
import at.procon.dip.search.engine.SearchEngine;
import at.procon.dip.search.repository.DocumentTrigramSearchRepository;
import at.procon.dip.search.config.DipSearchProperties;
import at.procon.dip.runtime.condition.ConditionalOnRuntimeMode;
import at.procon.dip.runtime.config.RuntimeMode;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@RequiredArgsConstructor
public class PostgresTrigramSearchEngine implements SearchEngine {

    private final DocumentTrigramSearchRepository repository;
    private final DipSearchProperties properties;

    @Override
    public SearchEngineType type() {
        return SearchEngineType.POSTGRES_TRIGRAM;
    }

    @Override
    public boolean supports(SearchExecutionContext context) {
        return context.getRequest().getQueryText() != null && !context.getRequest().getQueryText().isBlank();
    }

    @Override
    public List<SearchHit> execute(SearchExecutionContext context) {
        return repository.search(
                context,
                properties.getTrigramCandidateLimit(),
                properties.getTrigramSimilarityThreshold());
    }
}
