// Package config handles configuration loading for mosis-portal
package config

import (
	"os"
)

// Config holds all configuration for the portal
type Config struct {
	// Server settings
	ListenAddr string
	BaseURL    string

	// Database
	DatabasePath string

	// JWT
	JWTSecret string

	// OAuth2 - GitHub
	GitHubClientID     string
	GitHubClientSecret string

	// OAuth2 - Google
	GoogleClientID     string
	GoogleClientSecret string

	// Storage
	StoragePath string // Base path for all storage (packages/, assets/, temp/)
	BackupsDir  string
}

// Load reads configuration from environment variables with defaults
func Load() (*Config, error) {
	cfg := &Config{
		ListenAddr:   getEnv("LISTEN_ADDR", ":8080"),
		BaseURL:      getEnv("BASE_URL", "http://localhost:8080"),
		DatabasePath: getEnv("DATABASE_PATH", "./data/portal.db"),

		JWTSecret: getEnv("JWT_SECRET", "change-me-in-production"),

		GitHubClientID:     os.Getenv("GITHUB_CLIENT_ID"),
		GitHubClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),

		GoogleClientID:     os.Getenv("GOOGLE_CLIENT_ID"),
		GoogleClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),

		StoragePath: getEnv("STORAGE_PATH", "./storage"),
		BackupsDir:  getEnv("BACKUPS_DIR", "./backups"),
	}

	return cfg, nil
}

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