package tech.easyflow.ai.config;

import com.easyagents.engine.es.ElasticSearcher;
import com.easyagents.search.engine.lucene.LuceneSearcher;
import com.easyagents.search.engine.service.DocumentSearcher;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SearcherFactory {

    private final ObjectProvider<DocumentSearcher> documentSearcherProvider;

    public SearcherFactory(ObjectProvider<DocumentSearcher> documentSearcherProvider) {
        this.documentSearcherProvider = documentSearcherProvider;
    }

    @Bean
    @ConditionalOnProperty(prefix = "rag", name = "engine", havingValue = "LUCENE")
    public LuceneSearcher luceneSearcher(AiLuceneConfig luceneConfig) {
        return new LuceneSearcher(luceneConfig);
    }

    @Bean
    @ConditionalOnProperty(prefix = "rag", name = "engine", havingValue = "ES", matchIfMissing = true)
    public ElasticSearcher elasticSearcher(AiEsConfig aiEsConfig) {
        return new ElasticSearcher(aiEsConfig);
    }

    public DocumentSearcher getSearcher() {
        return documentSearcherProvider.getIfAvailable();
    }

    public DocumentSearcher getSearcher(String ignored) {
        return getSearcher();
    }
}
