package config

import (
	"os"
)

type Config struct {
	Port             string
	Env              string
	DatabaseDriver   string
	DatabaseHost     string
	DatabasePort     string
	DatabaseName     string
	DatabaseUser     string
	DatabasePassword string
	FrontendURL      string
}

func New() *Config {

	return generateConfig()
}

func generateConfig() *Config {

	cfg := Config{
		Port:             getEnv("PORT", "8080"),
		DatabaseDriver:   getEnv("DB_DRIVER", "postgres"),
		DatabaseHost:     getEnv("database", "localhost"),
		DatabasePort:     getEnv("DB_PORT", "5432"),
		DatabaseName:     getEnv("DB_NAME", "studia"),
		DatabaseUser:     getEnv("DB_USER", "user"),
		DatabasePassword: getEnv("DB_PASSWORD", "password"),
		Env:              getEnv("ENV", "development"),
		FrontendURL:      getEnv("FRONTEND_URL", "http://localhost:5173"),
	}

	return &cfg
}

// helper function to get env var or default
func getEnv(key, defaultVal string) string {
	value, exists := os.LookupEnv(key)
	if !exists || value == "" {
		return defaultVal
	}
	return value
}
