package de.accso.flexinale.ticketing.infrastructure;

import de.accso.flexinale.ticketing.api_out.event.OnlineKontingentChangedPublisher;
import de.accso.flexinale.ticketing.api_out.event.TicketPublisher;
import de.accso.flexinale.ticketing.application.services.TicketService;
import de.accso.flexinale.ticketing.domain.services.KontingentService;
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 BootstrappingPostConstructTicketing {
    private static final Logger LOGGER = LoggerFactory.getLogger(BootstrappingPostConstructTicketing.class);

    @Autowired
    TicketService ticketService;

    @Autowired
    TicketPublisher ticketPublisher;

    @Autowired
    KontingentService kontingentService;

    @Autowired
    OnlineKontingentChangedPublisher onlineKontingentChangedPublisher;

    // Alternatively annotate method with @EventListener(ApplicationReadyEvent.class)
    @PostConstruct
    public void postConstruct() {
        LOGGER.info("Publishing existing Tickets and Kontingente as events");

        loadAllTicketsFromDatabaseAndPublish();
        loadAllKontingenteFromDatabaseAndPublish();

        LOGGER.info("Publishing existing Tickets and Kontingente as events ... done");
    }

    private void loadAllKontingenteFromDatabaseAndPublish() {
        onlineKontingentChangedPublisher.publishOnlineKontingentChanged( kontingentService.kontingente() );
    }

    private void loadAllTicketsFromDatabaseAndPublish() {
        ticketPublisher.publishGekaufteTickets( ticketService.tickets() );
    }
}
