package de.avatic.lcc.config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

/**
 * TestContainers configuration for multi-database integration testing.
 * <p>
 * Automatically starts the correct database container based on active Spring profile.
 * Uses @ServiceConnection to automatically configure Spring DataSource.
 * <p>
 * Usage:
 * <pre>
 * mvn test -Dspring.profiles.active=test,mysql -Dtest=DatabaseConfigurationSmokeTest
 * mvn test -Dspring.profiles.active=test,mssql -Dtest=DatabaseConfigurationSmokeTest
 * </pre>
 */
@TestConfiguration
public class DatabaseTestConfiguration {

    @Bean
    @ServiceConnection
    @Profile("mysql")
    public MySQLContainer<?> mysqlContainer() {
        System.out.println("DatabaseTestConfiguration: Creating MySQL container bean...");
        MySQLContainer<?> container = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"))
                .withDatabaseName("lcc_test")
                .withUsername("test")
                .withPassword("test");
        System.out.println("DatabaseTestConfiguration: MySQL container bean created");
        return container;
    }

    @Bean
    @ServiceConnection
    @Profile("mssql")
    public MSSQLServerContainer<?> mssqlContainer() {
        System.out.println("DatabaseTestConfiguration: Creating MSSQL container bean...");
        MSSQLServerContainer<?> container = new MSSQLServerContainer<>(
                DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest"))
                .acceptLicense()
                .withPassword("YourStrong!Passw0rd123");
        System.out.println("DatabaseTestConfiguration: MSSQL container bean created");
        return container;
    }
}
