package at.procon.dip.domain.time.startup;

import at.procon.dip.domain.time.config.TimeDomainProperties;
import at.procon.dip.domain.time.service.LeitstandTimeProjectionService;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@ConditionalOnProperty(prefix = "dip.time.leitstand", name = {"enabled", "startup-projection-rebuild-enabled"}, havingValue = "true")
@RequiredArgsConstructor
@Slf4j
public class LeitstandTimeProjectionStartupRunner implements ApplicationRunner {

    private final TimeDomainProperties properties;
    private final LeitstandTimeProjectionService projectionService;

    @Override
    public void run(ApplicationArguments args) {
        log.info("Starting Leitstand TIME projection rebuild (scope={})", properties.getLeitstand().getScopeKey());
        int refreshed = projectionService.refreshAll();
        log.info("Completed Leitstand TIME projection rebuild. Refreshed {} time-entry projections", refreshed);
    }
}
