package it.enea.cruise.config;

import java.util.HashMap;

import javax.sql.DataSource;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

@SpringBootConfiguration
@EnableJpaRepositories(
  basePackages = {
		  "it.enea.cruise.model.structure",
		  "it.enea.cruise.service.structure",
		  "it.enea.cruise.model.user",
          "it.enea.cruise.repositories"},
  entityManagerFactoryRef = "userEntityManagerIDP",
  transactionManagerRef = "userTransactionManagerIDP"
)

@EntityScan(basePackages = {
		  "it.enea.cruise.model.structure",
		  "it.enea.cruise.model.user"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@RequiredArgsConstructor
public class userDBConfiguration {

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

    @Value("${spring.user-datasource-idp.hibernate.hbm2ddl}")
    private String hbmToDDL;
	
    @Primary
    @Bean
    @ConfigurationProperties("spring.user-datasource-idp")
    public DataSource userDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean userEntityManagerIDP() {
        LocalContainerEntityManagerFactoryBean em
          = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(userDataSource());
        em.setPackagesToScan(
                "it.enea.cruise.model.structure",
                "it.enea.cruise.service.structure",
                "it.enea.cruise.model.user",
                "it.enea.cruise.repositories");

        HibernateJpaVendorAdapter vendorAdapter
          = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        
		properties.put("hibernate.hbm2ddl.auto", hbmToDDL);
        properties.put("hibernate.dialect", dialect);
        em.setJpaPropertyMap(properties);

        return em;
    }
    
    @Bean
    @Primary
    public PlatformTransactionManager userTransactionManagerIDP() {
 
        JpaTransactionManager transactionManager
          = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
        		userEntityManagerIDP().getObject());
        return transactionManager;
    }
}