package org.endeavourhealth.enterprise.core.database;

import com.fasterxml.jackson.databind.JsonNode;
import org.endeavourhealth.common.config.ConfigManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public enum PersistenceManager {
    INSTANCE;
	private static final Logger LOG = LoggerFactory.getLogger(PersistenceManager.class);

    private EntityManagerFactory emEnterpriseAdmin;
    private EntityManagerFactory emEnterpriseData;

    private PersistenceManager() {

			Map<String, Object> override = getHibernateOverridesFromConfig("admin_database");
			emEnterpriseAdmin = Persistence.createEntityManagerFactory("enterprise_admin", override);

			override = getHibernateOverridesFromConfig("patient_database");
			emEnterpriseData = Persistence.createEntityManagerFactory("enterprise_data", override);
		}

	public EntityManager getEmEnterpriseAdmin() {
        return emEnterpriseAdmin.createEntityManager();
    }

    public EntityManager getEmEnterpriseData() {
        return emEnterpriseData.createEntityManager();
    }

    public void close() {
        emEnterpriseAdmin.close();
        emEnterpriseData.close();

    }

	private Map<String, Object> getHibernateOverridesFromConfig(String configId) {
		Map<String, Object> override = new HashMap<>();

		try {
			JsonNode config = ConfigManager.getConfigurationAsJson(configId);
			if (config.has("url"))
				override.put("hibernate.connection.url", config.get("url").asText());
			if (config.has("driverClass"))
				override.put("hibernate.connection.driver_class", config.get("driverClass").asText());
			if (config.has("username"))
				override.put("hibernate.connection.username", config.get("username").asText());
			if (config.has("password"))
				override.put("hibernate.connection.password", config.get("password").asText());
		} catch (Exception e) {
			LOG.warn("Error loading config ["+configId+"]", e);
		}
		return override;
	}
}

@@ -1564,108 +1564,9 @@
 d);%0A
-%09%09%09if (config.has(%22url%22))%0A%09%09%09%09override.put(%22hibernate.connection.url%22, config.get(%22url%22).asText());
 %0A
+
 %09%09%09i
@@ -1678,32 +1678,33 @@
 ss%22).asText());%0A
+%0A
 %09%09%09if (config.ha
@@ -1706,16 +1706,150 @@
 ig.has(%22
+enterprise_url%22))%0A%09%09%09%09override.put(%22hibernate.connection.url%22, config.get(%22enterprise_url%22).asText());%0A%0A%09%09%09if (config.has(%22enterprise_
 username
@@ -1914,16 +1914,27 @@
 ig.get(%22
+enterprise_
 username
@@ -1947,16 +1947,17 @@
 ext());%0A
+%0A
 %09%09%09if (c
@@ -1967,16 +1967,27 @@
 ig.has(%22
+enterprise_
 password
@@ -2052,16 +2052,27 @@
 ig.get(%22
+enterprise_
 password
@@ -2085,16 +2085,17 @@
 ext());%0A
+%0A
 %09%09%7D catc

Update persistence manager to use "enterprise_" prefix for admin and patient configs