package it.enea.cruise.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


import com.zaxxer.hikari.HikariDataSource;

import java.util.HashMap;

@SpringBootConfiguration
@EnableTransactionManagement
@EnableJpaRepositories(
  basePackages = {"it.enea.cruise.model.data",
		  "it.enea.cruise.service.data",
		  "it.enea.cruise.service.parametricQuery",
		  "it.enea.cruise.model.parametricQuery"},
  entityManagerFactoryRef = "dataEntityManagerIDP",
  transactionManagerRef = "dataTransactionManagerIDP")
@ComponentScan(basePackages = {"it.enea.cruise.model.data",
		"it.enea.cruise.service.data",
		"it.enea.cruise.service.parametricQuery",
		"it.enea.cruise.model.parametricQuery"})
@RequiredArgsConstructor
public class dataDBConfiguration {
    private final Environment env;

    @Value("${spring.data-datasource-idp.hibernate.dialect}")
    private String dialect;

    @Value("${spring.data-datasource-idp.hibernate.hbm2ddl}")
    private String hbmToDDL;

    @Bean
    @ConfigurationProperties("spring.data-datasource-idp")
    public HikariDataSource dataDataSource() {
    		return DataSourceBuilder.create()
                    .type(HikariDataSource.class)
                    .build();
    }
    
    @Bean
    public LocalContainerEntityManagerFactoryBean dataEntityManagerIDP() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan("it.enea.cruise.model.data",
                "it.enea.cruise.service.data",
                "it.enea.cruise.service.parametricQuery",
                "it.enea.cruise.model.parametricQuery");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());        
        em.setDataSource(dataDataSource());

        HashMap<String, Object> properties = new HashMap<>();

        properties.put("hibernate.hbm2ddl.auto", hbmToDDL);
        properties.put("hibernate.dialect", dialect);
        em.setJpaPropertyMap(properties);
        return em;
    }

    @Bean
    public PlatformTransactionManager dataTransactionManagerIDP() {
		return new DataSourceTransactionManager(dataDataSource());
    }

    @Bean(name = "jdbcTemplateData")
    public JdbcTemplate jdbcTemplateData() {
     return new JdbcTemplate(dataDataSource());
    }
    
}