package config

import (
	"fmt"
	"os"

	"github.com/joho/godotenv"
)

var Cfg Config

type Config struct {
	Debug               bool
	ColorizedOutput     bool
	Host                string
	Port                string
	LogManagerServerUrl string
	FolderPaths         FolderPaths
	MariaDB             MariaDB
}

type FolderPaths struct {
	PublicStatic string
}

type MariaDB struct {
	Hostname     string
	Port         string
	Username     string
	Password     string
	DatabaseName string
}

func LoadConfig() {
	// used to determine server was startet in docker or not
	if os.Getenv("DOCKER") == "" {
		fmt.Println("Load env from file")
		godotenv.Load(".env")
	} else {
		fmt.Println("Load env from system")
	}

	config := Config{
		Debug:               os.Getenv("DEBUG") == "true",
		ColorizedOutput:     os.Getenv("COLORIZED_OUTPUT") == "true",
		LogManagerServerUrl: os.Getenv("LOG_MANAGER_SERVER_URL"),
		FolderPaths: FolderPaths{
			PublicStatic: os.Getenv("FOLDER_PUBLIC_STATIC"),
		},
		MariaDB: MariaDB{
			Hostname:     os.Getenv("MARIADB_HOSTNAME"),
			Port:         os.Getenv("MARIADB_PORT"),
			Username:     os.Getenv("MARIADB_USERNAME"),
			Password:     os.Getenv("MARIADB_PASSWORD"),
			DatabaseName: os.Getenv("MARIADB_DATABASE_NAME"),
		},
	}

	// load default values if not in docker
	if os.Getenv("DOCKER") == "" {
		config.Host = os.Getenv("HOST")
		config.Port = os.Getenv("PORT")
	} else { // load from docker env
		config.Host = "0.0.0.0"
		config.Port = "80"
	}

	Cfg = config
}
