package com.betriebsratkanzlei.legalconsenthub.config

import com.github.dockerjava.api.model.ExposedPort
import com.github.dockerjava.api.model.HostConfig
import com.github.dockerjava.api.model.PortBinding
import com.github.dockerjava.api.model.Ports
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName

@Configuration
@Profile("testcontainers")
class TestContainersConfig {

    @Bean
    @ServiceConnection
    fun postgresContainer(): PostgreSQLContainer<*> {
        return PostgreSQLContainer(DockerImageName.parse("postgres:17-alpine"))
            .withDatabaseName("legalconsenthub")
            .withUsername("legalconsenthub")
            .withPassword("legalconsenthub")
            .withExposedPorts(5432)
            .withCreateContainerCmdModifier { cmd ->
                cmd.withName("legalconsenthub-backend")
                cmd.withHostConfig(
                    HostConfig().apply {
                        this.withPortBindings(
                            PortBinding(
                                Ports.Binding.bindPort(5432),
                                ExposedPort(5432)
                            )
                        )
                    }
                )
            }
            .withReuse(true)
    }
}
