package config

import (
	"os"
	"strings"

	"github.com/goraz/onion"
	"github.com/rs/zerolog"
)

/* GetConfLoader
* Get the configuration loader for the application
* Parameters:
* - AppName: string : the name of the application
* Returns:
* - *onion.Onion : the configuration loader
*  The configuration loader will load the configuration from the following sources:
*  - the environment variables with the prefix APPNAME_
*  - the file /etc/oc/appname.json
*  - the file ./appname.json
*  The configuration loader will merge the configuration from the different sources
*  The configuration loader will give priority to the environment variables
*  The configuration loader will give priority to the local file over the default file
 */

func GetConfLoader() *onion.Onion {
	logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
	AppName := GetAppName()
	EnvPrefix := "OC_"
	defaultConfigFile := "/etc/oc/" + AppName[3:] + ".json"
	localConfigFile := "./" + AppName[3:] + ".json"
	var configFile string
	var o *onion.Onion
	l3 := GetEnvVarLayer(EnvPrefix)
	l2, err := onion.NewFileLayer(localConfigFile, nil)
	if err == nil {
		logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
		configFile = localConfigFile
	}
	l1, err := onion.NewFileLayer(defaultConfigFile, nil)
	if err == nil {
		logger.Info().Msg("Config file found : " + defaultConfigFile)
		configFile = defaultConfigFile
	}
	if configFile == "" {
		logger.Info().Msg("No config file found, using env")
		o = onion.New(l3)
	} else if l1 != nil && l2 != nil {
		o = onion.New(l1, l2, l3)
	} else if l1 == nil {
		o = onion.New(l2, l3)
	} else if l2 == nil {
		o = onion.New(l1, l3)
	}
	return o
}

func GetEnvVarLayer(prefix string) onion.Layer {
	envVars := make(map[string]interface{})

	for _, e := range os.Environ() {
		pair := strings.SplitN(e, "=", 2)
		key := pair[0]
		if strings.HasPrefix(key, prefix) {
			envVars[strings.TrimPrefix(key, prefix)] = pair[1]
		}
	}

	return onion.NewMapLayer(envVars)
}