package io.quarkus.hibernate.orm.runtime.boot;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import org.hibernate.cfg.Environment;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;

/**
 * A lighter alternative to org.hibernate.cfg.Environment: we don't need it to look for certain environment variables,
 * and don't want it to copy all of {@link System#getProperties()} into the ORM configuration.
 * The only legacy behaviour we allow is to load the "/hibernate.properties" resource.
 */
public class QuarkusEnvironment {

    private static final Map GLOBAL_INITIAL_PROPERTIES;

    static {
        //Skip logging this as the original Environment initialization is also being triggered;
        //avoiding that will need ORM6.
        //Version.logVersion();

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader != null) {
            InputStream stream = classLoader.getResourceAsStream("/hibernate.properties");
            if (stream != null) {
                final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, Environment.class.getName());
                LOG.warnf(
                        "Resource /hibernate.properties was found. This configuration source is deprecated and will be removed in a future version of Quarkus, "
                                +
                                "as it's not compatible with Live-Reloading, with multiple Persistence Units and many more cool features we have planned. Please try to stop "
                                +
                                "using this to configure your ORM and report anything you will need to do so: https://github.com/quarkusio/quarkus/issues/ . Thanks! ");
                final Properties p = new Properties();
                try {
                    p.load(stream);
                } catch (Exception e) {
                    LOG.unableToLoadProperties();
                } finally {
                    try {
                        stream.close();
                    } catch (IOException ioe) {
                        LOG.unableToCloseStreamError(ioe);
                    }
                }
                GLOBAL_INITIAL_PROPERTIES = Collections.unmodifiableMap(p);
            } else {
                GLOBAL_INITIAL_PROPERTIES = Collections.EMPTY_MAP;
            }
        } else {
            GLOBAL_INITIAL_PROPERTIES = Collections.EMPTY_MAP;
        }
    }

    /**
     * @return
     * @see Environment#getProperties()
     */
    public static Map getInitialProperties() {
        return GLOBAL_INITIAL_PROPERTIES;
    }
}

@@ -1306,17 +1306,21 @@
    %22
-R
+The r
 esource 
 /hib
@@ -1315,17 +1315,16 @@
 esource 
-/
 hibernat
@@ -1345,16 +1345,48 @@
 as found
+ in the root of your application
 . This c
@@ -1576,24 +1576,16 @@
 Live
--Reloading, with
+ Coding,
  mul
@@ -1664,19 +1664,20 @@
 ase 
-try to stop
+refrain from
  %22%0A 
@@ -1748,20 +1748,18 @@
  %22using 
+i
 t
-his
  to conf
@@ -1781,46 +1781,229 @@
 and 
-report anything you will need to do so
+use the available configurations in application.properties (check the guide in https://quarkus.io/guides/hibernate-orm#quarkus-hibernate-orm_configuration). Should no configuration exist for your use case please report in
 : ht
@@ -2048,17 +2048,19 @@
 ues/
- 
 . Thank
-s
+ you
 ! %22)

Update extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/QuarkusEnvironment.java