package com.cameleer.server.app.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class ClickHouseSchemaInitializer {

    private static final Logger log = LoggerFactory.getLogger(ClickHouseSchemaInitializer.class);

    private final JdbcTemplate clickHouseJdbc;

    public ClickHouseSchemaInitializer(
            @Qualifier("clickHouseJdbcTemplate") JdbcTemplate clickHouseJdbc) {
        this.clickHouseJdbc = clickHouseJdbc;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void initializeSchema() {
        try {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource script = resolver.getResource("classpath:clickhouse/init.sql");

            String sql = script.getContentAsString(StandardCharsets.UTF_8);
            log.info("Executing ClickHouse schema: {}", script.getFilename());
            for (String statement : sql.split(";")) {
                String trimmed = statement.trim();
                // Skip empty segments and comment-only segments
                String withoutComments = trimmed.lines()
                        .filter(line -> !line.stripLeading().startsWith("--"))
                        .map(String::trim)
                        .filter(line -> !line.isEmpty())
                        .reduce("", (a, b) -> a + b);
                if (!withoutComments.isEmpty()) {
                    clickHouseJdbc.execute(trimmed);
                }
            }

            log.info("ClickHouse schema initialization complete");
        } catch (Exception e) {
            log.error("ClickHouse schema initialization failed — server will continue but ClickHouse features may not work", e);
        }
    }
}
