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

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.DocumentFullTextSearchRepository;
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 PostgresFullTextSearchEngine implements SearchEngine {

    private final DocumentFullTextSearchRepository repository;
    private final DipSearchProperties properties;

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

    @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.getFulltextCandidateLimit());
    }
}
