package gc.mda.signal_batch.global.config;

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 조회 전용 서버 DataSource 설정
 * - query 프로파일에서만 활성화
 * - application-query.yml의 spring.datasource 설정 사용
 */
@Slf4j
@Configuration
@Profile("query")
public class QueryDataSourceConfig {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.hikari")
    public DataSource dataSource(DataSourceProperties properties) {
        log.info("Creating primary DataSource for query-only server");
        HikariDataSource dataSource = properties.initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
        log.info("Query DataSource configured: {}", properties.getUrl());
        return dataSource;
    }

    @Bean(name = "queryDataSource")
    public DataSource queryDataSource(@Qualifier("dataSource") DataSource dataSource) {
        log.info("Aliasing primary DataSource as queryDataSource");
        return dataSource;
    }

    @Bean(name = "queryJdbcTemplate")
    public JdbcTemplate queryJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.setQueryTimeout(300);
        log.info("QueryJdbcTemplate created for query-only server");
        return jdbcTemplate;
    }

    @Bean(name = "queryNamedJdbcTemplate")
    public NamedParameterJdbcTemplate queryNamedJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean(name = "queryTransactionManager")
    @Primary
    public PlatformTransactionManager queryTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // collectJdbcTemplate이 필요한 경우를 위한 별칭 (query DB를 사용)
    @Bean(name = "collectJdbcTemplate")
    public JdbcTemplate collectJdbcTemplate(@Qualifier("queryJdbcTemplate") JdbcTemplate queryJdbcTemplate) {
        log.info("CollectJdbcTemplate aliased to QueryJdbcTemplate (query-only mode)");
        return queryJdbcTemplate;
    }

    @Bean(name = "collectNamedJdbcTemplate")
    public NamedParameterJdbcTemplate collectNamedJdbcTemplate(@Qualifier("queryNamedJdbcTemplate") NamedParameterJdbcTemplate queryNamedJdbcTemplate) {
        return queryNamedJdbcTemplate;
    }

    @Bean(name = "collectDataSource")
    public DataSource collectDataSource(@Qualifier("dataSource") DataSource dataSource) {
        log.info("CollectDataSource aliased to primary DataSource (query-only mode)");
        return dataSource;
    }
}
