package dp.kotul.templategroupsbackend

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.jdbc.datasource.DriverManagerDataSource
import org.springframework.transaction.annotation.EnableTransactionManagement
import org.testcontainers.containers.PostgreSQLContainer
import javax.sql.DataSource

const val DOCKER_IMAGE = "postgres:9.3.24"

class KPostgreSQLContainer(imageName: String) : PostgreSQLContainer<KPostgreSQLContainer>(imageName)

@Configuration
@EnableTransactionManagement
@Profile("repository-test")
class PostgreSQLContainerDatabase {

    @Bean
    fun dataSource(): DataSource {
        val container: KPostgreSQLContainer = KPostgreSQLContainer(DOCKER_IMAGE)
                .withDatabaseName("tgtest")
                .withUsername("tgusertest")
                .withPassword("test")
                .withExposedPorts(1521)

        container.start()
        val ds = DriverManagerDataSource()
        ds.setDriverClassName("org.postgresql.Driver")
        ds.url = container.jdbcUrl
        ds.username = container.username
        ds.password = container.password
        return ds
    }
}
