package com.cameleer.server.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.clickhouse.ClickHouseContainer;
import org.testcontainers.containers.PostgreSQLContainer;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public abstract class AbstractPostgresIT {

    static final PostgreSQLContainer<?> postgres;
    static final ClickHouseContainer clickhouse;

    static {
        postgres = new PostgreSQLContainer<>("postgres:16")
                .withDatabaseName("cameleer")
                .withUsername("cameleer")
                .withPassword("test")
                .withReuse(true);
        postgres.start();

        clickhouse = new ClickHouseContainer("clickhouse/clickhouse-server:24.12")
                .withReuse(true);
        clickhouse.start();
    }

    @Autowired
    protected JdbcTemplate jdbcTemplate;

    @DynamicPropertySource
    static void configureProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgres::getJdbcUrl);
        registry.add("spring.datasource.username", postgres::getUsername);
        registry.add("spring.datasource.password", postgres::getPassword);
        registry.add("spring.datasource.driver-class-name", () -> "org.postgresql.Driver");
        registry.add("spring.flyway.enabled", () -> "true");
        registry.add("spring.flyway.url", postgres::getJdbcUrl);
        registry.add("spring.flyway.user", postgres::getUsername);
        registry.add("spring.flyway.password", postgres::getPassword);
        registry.add("cameleer.server.clickhouse.url", clickhouse::getJdbcUrl);
        registry.add("cameleer.server.clickhouse.username", clickhouse::getUsername);
        registry.add("cameleer.server.clickhouse.password", clickhouse::getPassword);
    }
}
