package testdata;

import de.accso.flexinale.FlexinaleDistributedApplicationTestdata;
import de.accso.flexinale.common.shared_kernel.DoNotCheckInArchitectureTests;
import de.accso.flexinale.security.infrastructure.persistence.BenutzerDao;
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 BenutzerDatabaseCleaner {

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

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

        @Test
        public void deleteAllBenutzerFromDatabase() {
            internal_deleteAllBenutzerFromDatabase(benutzerDao);
        }
    }

    @Nested
    @SpringBootTest(classes = {FlexinaleDistributedApplicationTestdata.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
    @ActiveProfiles({ "testdata", "testdata-besucherportal" })
    public class BenutzerDatabaseCleanerForBesucherportal {
        @Autowired
        private BenutzerDao benutzerDao;

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

        @Test
        public void deleteAllBenutzerFromDatabase() {
            internal_deleteAllBenutzerFromDatabase(benutzerDao);
        }
    }

    static void internal_deleteAllBenutzerFromDatabase(final BenutzerDao benutzerDao) {
        benutzerDao.deleteAll();
    }
}
