package config

import (
	"os"
	"strconv"
	"time"
)

type Config struct {
	DBPath        string
	MaxWorkers    int
	ReconcileTick time.Duration
	HTTPPort      string
	Kubeconfig    string
	Namespace     string
}

func Load() *Config {
	cfg := &Config{
		DBPath:        getEnv("DB_PATH", "./manager.db"),
		MaxWorkers:    getEnvInt("MAX_WORKERS", 2),
		ReconcileTick: getEnvDuration("RECONCILE_TICK", 2*time.Second),
		HTTPPort:      getEnv("HTTP_PORT", "8080"),
		Kubeconfig:    getEnv("KUBECONFIG", ".env/cluster1.yaml"),
		Namespace:     getEnv("NAMESPACE", "default"),
	}

	return cfg
}

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

func getEnvInt(key string, defaultValue int) int {
	if value := os.Getenv(key); value != "" {
		if intValue, err := strconv.Atoi(value); err == nil {
			return intValue
		}
	}
	return defaultValue
}

func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
	if value := os.Getenv(key); value != "" {
		if intValue, err := strconv.Atoi(value); err == nil {
			return time.Duration(intValue) * time.Second
		}
	}
	return defaultValue
}
