package conf

import (
	"sync"

	"github.com/beego/beego/logs"
	"github.com/goraz/onion"
)

type Config struct {
	MonitorPath string
	MongoUrl    string
	DBName      string
	Logs        string
	LokiUrl     string
	NatsUrl     string
}

var instance *Config
var once sync.Once

const defaultConfigFile = "/etc/oc/schedulerd.json"

func init() {
	configFile := ""
	var o *onion.Onion

	l3 := onion.NewEnvLayerPrefix("_", "OCSCHEDULERD_")
	l2, err := onion.NewFileLayer(defaultConfigFile, nil)
	if err == nil {
		logs.Info("Config file found : " + defaultConfigFile)
		configFile = defaultConfigFile
	}
	if configFile == "" || l2 == nil {
		logs.Info("No config file found, using env")
		o = onion.New(l3)
	} else {
		o = onion.New(l2, l3)
	}
	GetConfig().MonitorPath = o.GetStringDefault("MONITORD_PATH", "../oc-monitord/oc-monitord")
	GetConfig().Logs = o.GetStringDefault("LOG_LEVEL", "info")
	GetConfig().LokiUrl = o.GetStringDefault("LOKI_URL", "http://127.0.0.1:3100")
	GetConfig().NatsUrl = o.GetStringDefault("NATS_URL", "http://127.0.0.1:4222")
	GetConfig().MongoUrl = o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017")
	GetConfig().DBName = o.GetStringDefault("MONGO_DATABASE", "DC_myDC")

}

func GetConfig() *Config {
	once.Do(func() {
		instance = &Config{}
	})
	return instance
}
