package config

import (
	"os"
	"path/filepath"
)

type Config struct {
	Address       string
	DatabasePath  string
	LogArchiveDir string
	SessionKey    string
	EncryptionKey string
	OrgName       string
	BaseURL       string
	DefaultTheme  string
	DefaultMode   string
	RefreshCron   string
}

func Load() Config {
	return Config{
		Address:       env("MAINTAINARR_ADDR", ":8080"),
		DatabasePath:  env("MAINTAINARR_DB_PATH", filepath.Join("data", "maintainarr.db")),
		LogArchiveDir: env("MAINTAINARR_LOG_ARCHIVE_DIR", filepath.Join("data", "log-archives")),
		SessionKey:    env("MAINTAINARR_SESSION_KEY", "change-me-session-key-please"),
		EncryptionKey: env("MAINTAINARR_ENCRYPTION_KEY", "change-me-encryption-key-32bytes"),
		OrgName:       env("MAINTAINARR_ORG_NAME", "Maintainarr"),
		BaseURL:       env("MAINTAINARR_BASE_URL", "http://localhost:8080"),
		DefaultTheme:  env("MAINTAINARR_THEME", "dark"),
		DefaultMode:   env("MAINTAINARR_THEME_MODE", "dark"),
		RefreshCron:   env("MAINTAINARR_REFRESH_CRON", "@every 5s"),
	}
}

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

	return fallback
}
