package at.procon.dip.search.service;

import at.procon.dip.runtime.condition.ConditionalOnRuntimeMode;
import at.procon.dip.runtime.config.RuntimeMode;
import at.procon.dip.search.config.DipSearchProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@RequiredArgsConstructor
@Slf4j
public class SearchLexicalIndexScheduler {

    private final DipSearchProperties properties;
    private final DocumentLexicalIndexService lexicalIndexService;

    @Scheduled(fixedDelayString = "${dip.search.scheduled-lexical-backfill-delay-ms:30000}")
    public void backfillMissingLexicalIndexes() {
        if (!properties.isScheduledLexicalBackfillEnabled()) {
            return;
        }
        int updated = lexicalIndexService.backfillMissingVectors(properties.getScheduledLexicalBackfillBatchSize());
        if (updated > 0) {
            log.info("Search lexical index scheduled backfill updated {} representations", updated);
        }
    }
}
