package com.cameleer.server.app;

import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Loads and executes the consolidated ClickHouse init.sql schema for integration tests.
 */
public final class ClickHouseTestHelper {

    private ClickHouseTestHelper() {}

    public static void executeInitSql(JdbcTemplate jdbc) throws IOException {
        String sql = new ClassPathResource("clickhouse/init.sql")
                .getContentAsString(StandardCharsets.UTF_8);
        for (String statement : sql.split(";")) {
            String trimmed = statement.trim();
            String withoutComments = trimmed.lines()
                    .filter(line -> !line.stripLeading().startsWith("--"))
                    .map(String::trim)
                    .filter(line -> !line.isEmpty())
                    .reduce("", (a, b) -> a + b);
            if (!withoutComments.isEmpty()) {
                jdbc.execute(trimmed);
            }
        }
    }
}
