package testdata;

import de.accso.flexinale.FlexinaleDistributedApplicationTestdata;
import de.accso.flexinale.backoffice.domain.dao.FilmDao;
import de.accso.flexinale.backoffice.domain.dao.KinoDao;
import de.accso.flexinale.backoffice.domain.dao.VorfuehrungDao;
import de.accso.flexinale.common.shared_kernel.DoNotCheckInArchitectureTests;
import de.accso.flexinale.security.infrastructure.persistence.BenutzerDao;
import de.accso.flexinale.ticketing.domain.dao.KontingentDao;
import de.accso.flexinale.ticketing.domain.dao.TicketDao;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

import static testdata.DatabaseConfig.*;

@DoNotCheckInArchitectureTests
@SuppressWarnings("NewClassNamingConvention")
class AllDatabaseCleaner {

    @Nested
    @SpringBootTest(classes = {FlexinaleDistributedApplicationTestdata.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
    @ActiveProfiles({ "testdata", "testdata-backoffice" })
    public class AllDatabaseCleanerForBackoffice {
        @Autowired
        private KinoDao kinoDao;

        @Autowired
        private FilmDao filmDao;

        @Autowired
        private VorfuehrungDao vorfuehrungDao;

        @Autowired
        private BenutzerDao benutzerDao;

        @DynamicPropertySource // @TestPropertySource would work as well
        static void registerDynamicProperties(DynamicPropertyRegistry registry) {
            registry.add(dataBaseUrlKey, () -> databaseBackoffice);
            registry.add(dataBaseDDLAutoKey, () -> dataBaseDDLAuto_Create);
        }

        @Test
        public void deleteAllFilmeAndKinosAndVorfuehrungenFromDatabase() {
            // as we set the spring.jpa.hibernate.ddl-auto to create, the data is already cleaned,
            // hence the following call is actually useless

            FKKsVDatabaseCleaner.internal_deleteAllFilmeAndKinosAndVorfuehrungenFromDatabase(
                    kinoDao, filmDao, vorfuehrungDao);
        }

        @Test
        public void deleteAllBenutzerFromDatabase() {
            // as we set the spring.jpa.hibernate.ddl-auto to create, the data is already cleaned,
            // hence the following call is actually useless

            BenutzerDatabaseCleaner.internal_deleteAllBenutzerFromDatabase(
                    benutzerDao);
        }
    }

    @Nested
    @SpringBootTest(classes = {FlexinaleDistributedApplicationTestdata.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
    @ActiveProfiles({ "testdata", "testdata-besucherportal" })
    public class AllDatabaseCleanerForBesucherportal {

        @Autowired
        private BenutzerDao benutzerDao;

        @DynamicPropertySource // @TestPropertySource would work as well
        static void registerDynamicProperties(DynamicPropertyRegistry registry) {
            registry.add(dataBaseUrlKey, () -> databaseBesucherportal);
            registry.add(dataBaseDDLAutoKey, () -> dataBaseDDLAuto_Create);
        }

        @Test
        public void deleteAllBenutzerFromDatabase() {
            // as we set the spring.jpa.hibernate.ddl-auto to create, the data is already cleaned,
            // hence the following call is atually useless

            BenutzerDatabaseCleaner.internal_deleteAllBenutzerFromDatabase(
                    benutzerDao);
        }
    }

    @Nested
    @SpringBootTest(classes = {FlexinaleDistributedApplicationTestdata.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
    @ActiveProfiles({ "testdata", "testdata-ticketing" })
    public class AllDatabaseCleanerForTicketing {
        @Autowired
        private TicketDao ticketDao;

        @Autowired
        private KontingentDao kontingentDao;

        @DynamicPropertySource // @TestPropertySource would work as well
        static void registerDynamicProperties(DynamicPropertyRegistry registry) {
            registry.add(dataBaseUrlKey, () -> databaseTicketing);
            registry.add(dataBaseDDLAutoKey, () -> dataBaseDDLAuto_Create);
        }

        @Test
        public void deleteAllTicketsAndKontingenteFromDatabase() {
            // as we set the spring.jpa.hibernate.ddl-auto to create, the data is already cleaned,
            // hence the following call is atually useless

            TicketsAndKontingenteDatabaseCleaner.internal_deleteAllTicketsAndKontingenteFromDatabase(
                    ticketDao, kontingentDao);
        }
    }
}
