package config

import (
	"fmt"
	"log"
	"os"
	"strconv"

	"github.com/joho/godotenv"
)

var Cfg Config

type Config struct {
	Debug            bool
	Host             string
	Port             string
	LogFolder        string
	SSEServerEnabled bool
	DaysToKeepLogs   int
}

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")
	}

	daysToKeepLogs, err := strconv.Atoi(os.Getenv("DAYS_TO_KEEP_LOGS"))

	if err != nil {
		log.Fatalf("Failed to load DAYS_TO_KEEP_LOGS from env, err: %s", err.Error())
	}

	Cfg = Config{
		Debug:            os.Getenv("DEBUG") == "true",
		Host:             os.Getenv("HOST"),
		Port:             os.Getenv("PORT"),
		LogFolder:        os.Getenv("LOG_FOLDER"),
		SSEServerEnabled: os.Getenv("SSE_SERVER_ENABLED") == "true",
		DaysToKeepLogs:   daysToKeepLogs,
	}
}
