package at.procon.dip.migration.startup;

import at.procon.dip.migration.config.LegacyTedBackfillProperties;
import at.procon.dip.migration.service.LegacyTedBackfillMigrationService;
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.stereotype.Component;

@Component
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@RequiredArgsConstructor
@Slf4j
public class LegacyTedBackfillStartupRunner implements ApplicationRunner {

    private final LegacyTedBackfillProperties properties;
    private final LegacyTedBackfillMigrationService migrationService;

    @Override
    public void run(ApplicationArguments args) {
        if (!properties.isEnabled() || !properties.isStartupEnabled()) {
            return;
        }

        log.info("Startup-triggered legacy TED backfill is enabled");
        migrationService.runBackfill();
    }
}
