package at.procon.dip.embedding.job;

import at.procon.dip.embedding.service.RepresentationEmbeddingOrchestrator;
import at.procon.dip.runtime.condition.ConditionalOnRuntimeMode;
import at.procon.dip.runtime.config.RuntimeMode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@ConditionalOnProperty(prefix = "dip.embedding.jobs", name = "enabled", havingValue = "true")
public class EmbeddingJobScheduler {

    private final RepresentationEmbeddingOrchestrator orchestrator;

    @Scheduled(fixedDelayString = "${dip.embedding.jobs.scheduler-delay-ms:5000}")
    public void processNextBatch() {
        try {
            int processed = orchestrator.processNextReadyBatch();
            if (processed > 0) {
                log.debug("NEW runtime embedding job scheduler processed {} job(s)", processed);
            }
        } catch (Exception ex) {
            log.warn("NEW runtime embedding job scheduler failed: {}", ex.getMessage(), ex);
        }
    }
}
