package config

import "os"

type Config struct {
	HTTPAddress     string
	StatusStorePath string
	DatabaseURL     string
	UploadDir       string
	// MQTT — optional.  When MQTTBroker is empty, notifications are disabled.
	MQTTBroker   string
	MQTTUsername string
	MQTTPassword string
	// Auth — optional.  When AdminPassword is empty, EnsureAdminUser is skipped.
	AdminPassword      string // MORZ_INFOBOARD_ADMIN_PASSWORD
	DefaultTenantSlug  string // MORZ_INFOBOARD_DEFAULT_TENANT (default: "morz")
	DevMode            bool   // MORZ_INFOBOARD_DEV_MODE — when true, session cookie works without HTTPS
}

func Load() Config {
	return Config{
		HTTPAddress:        getenv("MORZ_INFOBOARD_HTTP_ADDR", ":8080"),
		StatusStorePath:    os.Getenv("MORZ_INFOBOARD_STATUS_STORE_PATH"),
		DatabaseURL:        getenv("MORZ_INFOBOARD_DATABASE_URL", "postgres://morz_infoboard:morz_infoboard@localhost:5432/morz_infoboard?sslmode=disable"),
		UploadDir:          getenv("MORZ_INFOBOARD_UPLOAD_DIR", "/tmp/morz-uploads"),
		MQTTBroker:         os.Getenv("MORZ_INFOBOARD_MQTT_BROKER"),
		MQTTUsername:       os.Getenv("MORZ_INFOBOARD_MQTT_USERNAME"),
		MQTTPassword:       os.Getenv("MORZ_INFOBOARD_MQTT_PASSWORD"),
		AdminPassword:      os.Getenv("MORZ_INFOBOARD_ADMIN_PASSWORD"),
		DefaultTenantSlug:  getenv("MORZ_INFOBOARD_DEFAULT_TENANT", "morz"),
		DevMode:            os.Getenv("MORZ_INFOBOARD_DEV_MODE") == "true",
	}
}

func getenv(key, fallback string) string {
	value := os.Getenv(key)
	if value == "" {
		return fallback
	}

	return value
}
