package com.eactive.eai.rms.data.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.hibernate.cfg.AvailableSettings;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
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.support.SharedEntityManagerBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.eactive.eai.common.dao.Keys;
import com.eactive.eai.data.jpa.BaseRepositoryImpl;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.data.EMSDataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "EMSAuditorAware")
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryForEMS", transactionManagerRef = "transactionManagerForEMS", basePackages = {"com.eactive.eai", "com.eactive.apim.portal"}, repositoryBaseClass = BaseRepositoryImpl.class, includeFilters = @Filter(classes = EMSDataSource.class))
public class EMSTenantConfiguration {

	@Bean("entityManagerFactoryForEMS")
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryForEMS(
			@Qualifier(DataSourceTypeManager.MONITORING) DataSource dataSource,
			@Qualifier("hibernateProperties") Properties hibernateProperties) {
		
		LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
		bean.setDataSource(dataSource);
		bean.setPackagesToScan("com.eactive.eai.data", "com.eactive.eai.rms.data", "com.eactive.apim.portal");
		bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
		bean.setJpaProperties(hibernateProperties);
		
		Map<String, Object> settings = new HashMap<>();
		settings.put(AvailableSettings.DEFAULT_SCHEMA, System.getProperty(Keys.TABLE_OWNER_KEY));
		bean.setJpaPropertyMap(settings);
		
		return bean;
	}

	@Bean("transactionManagerForEMS")
	public TransactionManager transactionManager(@Qualifier(DataSourceTypeManager.MONITORING) DataSource dataSource,
												 @Qualifier("entityManagerFactoryForEMS") EntityManagerFactory entityManagerFactory) {
		JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
		jpaTransactionManager.setDataSource(dataSource);
		jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
		return jpaTransactionManager;
	}
	
	@Bean("entityManagerForEMS")
	public SharedEntityManagerBean entityManager(@Qualifier("entityManagerFactoryForEMS") EntityManagerFactory entityManagerFactory) {
		SharedEntityManagerBean sharedEntityManagerBean = new SharedEntityManagerBean();
		sharedEntityManagerBean.setEntityManagerFactory(entityManagerFactory);
		return sharedEntityManagerBean;
	}

}
