package de.accso.flexinale.ticketing.infrastructure;

import de.accso.flexinale.common.application.Config;
import de.accso.flexinale.ticketing.application.services.TicketRetrieverService;
import de.accso.flexinale.ticketing.application.services.TicketService;
import de.accso.flexinale.ticketing.domain.dao.KontingentDao;
import de.accso.flexinale.ticketing.domain.dao.TicketDao;
import de.accso.flexinale.ticketing.domain.services.KontingentService;
import de.accso.flexinale.ticketing.infrastructure.persistence.KontingentJpaRepository;
import de.accso.flexinale.ticketing.infrastructure.persistence.KontingentJpaRepositoryDelegate;
import de.accso.flexinale.ticketing.infrastructure.persistence.TicketJpaRepository;
import de.accso.flexinale.ticketing.infrastructure.persistence.TicketJpaRepositoryDelegate;
import de.accso.flexinale.ticketing_api_contract.api_contract.TicketRetriever;
import de.accso.flexinale.ticketing_api_contract.api_contract.Ticketing;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories({"de.accso.flexinale.ticketing.infrastructure.persistence"})
@EnableTransactionManagement
@EntityScan(basePackages={"de.accso.flexinale.ticketing.infrastructure.persistence"})
public class FlexinaleTicketingSpringFactory {

    @Bean
    public KontingentDao createTicketingKontingentDao(final KontingentJpaRepository kontingentJpaRepository) {
        return new KontingentJpaRepositoryDelegate(kontingentJpaRepository);
    }

    @Bean
    public TicketDao createTicketingTicketDao(final TicketJpaRepository ticketJpaRepository) {
        return new TicketJpaRepositoryDelegate(ticketJpaRepository);
    }

    // ------------------------------------------------------------------------------------------------

    @Bean
    public Ticketing createTicketingTicketingService(final de.accso.flexinale.ticketing.domain.services.TicketService ticketService, final KontingentService kontingentService) {
        return new TicketService(ticketService, kontingentService);
    }

    @Bean
    public TicketRetriever createTicketingTicketRetriever(final TicketDao ticketDao) {
        return new TicketRetrieverService(ticketDao);
    }

    // ------------------------------------------------------------------------------------------------

    @Bean
    public de.accso.flexinale.ticketing.domain.services.TicketService createTicketingTicketService(final TicketDao ticketDao,
                                                                                                   final KontingentDao kontingentDao,
                                                                                                   final Config config) {
        return new de.accso.flexinale.ticketing.domain.services.TicketService(ticketDao,  kontingentDao, config.getQuoteOnline());
    }

    @Bean
    public KontingentService createTicketingKontingentService(final KontingentDao kontingentDao) {
        return new KontingentService(kontingentDao);
    }
}
