package at.procon.dip.migration.startup;

import at.procon.dip.migration.config.LegacyTedEmbeddingBackfillProperties;
import at.procon.dip.migration.service.LegacyTedEmbeddingBackfillService;
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.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@RequiredArgsConstructor
@Order(Ordered.LOWEST_PRECEDENCE)
@Slf4j
public class LegacyTedEmbeddingBackfillStartupRunner implements ApplicationRunner {

    private final LegacyTedEmbeddingBackfillProperties properties;
    private final LegacyTedEmbeddingBackfillService backfillService;

    @Override
    public void run(ApplicationArguments args) {
        if (!properties.isEnabled() || !properties.isStartupEnabled()) {
            return;
        }
        log.info("Startup-triggered legacy TED embedding-only backfill is enabled");
        backfillService.runBackfill();
    }
}
