package com.utopiaindustries.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {


    @Bean(name = "dataSourceUind")
    @ConfigurationProperties(prefix = "spring.uinddatasource")
    public DataSource uindDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcTemplateUind")
    public JdbcTemplate uindJdbcTemplate(@Qualifier("dataSourceUind") DataSource ds ) {
        return new JdbcTemplate( ds );
    }

    @Bean(name = "namedParameterJdbcTemplateUind")
    public NamedParameterJdbcTemplate uindNamedParameterJdbcTemplate(@Qualifier( "dataSourceUind" ) DataSource ds ) {
        return new NamedParameterJdbcTemplate( ds );
    }

    /* COSMOS */

//    @Bean(name = "dataSourceCosmos")
//    @ConfigurationProperties(prefix = "spring.cosmosdatasource")
//    public DataSource cosmosDataSource() {
//        return DataSourceBuilder.create().build();
//    }
//
//
//    @Bean(name = "jdbcTemplateCosmos")
//    public JdbcTemplate cosmosJdbcTemplate( @Qualifier( "dataSourceCosmos" ) DataSource ds ) {
//        return new JdbcTemplate( ds );
//    }
//
//    @Bean(name = "namedParameterJdbcTemplateCosmos")
//    public NamedParameterJdbcTemplate cosmosNamedParameterJdbcTemplate( @Qualifier( "dataSourceCosmos" ) DataSource ds ) {
//        return new NamedParameterJdbcTemplate( ds );
//    }

    /*  LOCAL  */

    @Primary
    @Bean(name = "dataSourceLocal")
    @ConfigurationProperties(prefix = "spring.localdatasource")
    public DataSource localDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Primary
    @Bean(name = "jdbcTemplateLocal")
    public JdbcTemplate localJdbcTemplate( @Qualifier( "dataSourceLocal" ) DataSource ds ) {
        return new JdbcTemplate( ds );
    }

    @Primary
    @Bean(name = "namedParameterJdbcTemplateLocal")
    public NamedParameterJdbcTemplate localNamedParameterJdbcTemplate( @Qualifier( "dataSourceLocal" ) DataSource ds ) {
        return new NamedParameterJdbcTemplate( ds );
    }
}
