package de.accso.flexinale.backoffice.api_out.event;

import de.accso.flexinale.backoffice.application.services.FilmService;
import de.accso.flexinale.backoffice.application.services.KinoService;
import de.accso.flexinale.backoffice.application.services.VorfuehrungService;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile({"!test-integrated & !testdata"})
public class BootstrappingPostConstructBackoffice {
    private static final Logger LOGGER = LoggerFactory.getLogger(BootstrappingPostConstructBackoffice.class);

    @Autowired
    FilmService filmService;

    @Autowired
    FilmPublisher filmPublisher;

    @Autowired
    KinoService kinoService;

    @Autowired
    KinoPublisher kinoPublisher;

    @Autowired
    VorfuehrungService vorfuehrungService;

    @Autowired
    VorfuehrungPublisher vorfuehrungPublisher;

    // Alternatively annotate method with @EventListener(ApplicationReadyEvent.class)
    @PostConstruct
    public void postConstruct() {
        LOGGER.info("Publishing existing data for FKKsV as *Created events");

        loadAllFilmeFromDatabaseAndPublish();
        loadAllKinosFromDatabaseAndPublish();
        loadAllVorfuehrungenFromDatabaseAndPublish();

        LOGGER.info("Publishing existing data for FKKsV as *Created events ... done");
    }

    private void loadAllVorfuehrungenFromDatabaseAndPublish() {
        filmPublisher.publishNewFilms( filmService.filme() );
    }

    private void loadAllKinosFromDatabaseAndPublish() {
        kinoPublisher.publishNewKinos( kinoService.kinos() );
    }

    private void loadAllFilmeFromDatabaseAndPublish() {
        vorfuehrungPublisher.publishNewVorfuehrungen( vorfuehrungService.vorfuehrungen() );
    }
}
