package config

import (
	"fmt"
	"os"
)

type Config struct {
	ServerAddr       string
	DatabaseURL      string
	OIDCIssuerURL    string
	OIDCRedirectURL  string
	OIDCClientID     string
	OIDCClientSecret string
	JWTSecret        string
	NextcloudURL     string
	NextcloudUser    string
	NextcloudPass    string
	NextcloudBase    string
	AllowedOrigins   string
}

func Load() *Config {
	cfg := &Config{
		ServerAddr:       getEnv("SERVER_ADDR", ":8080"),
		DatabaseURL:      os.Getenv("DATABASE_URL"),
		OIDCIssuerURL:    os.Getenv("OIDC_ISSUER_URL"),
		OIDCRedirectURL:  os.Getenv("OIDC_REDIRECT_URL"),
		OIDCClientID:     os.Getenv("OIDC_CLIENT_ID"),
		OIDCClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
		JWTSecret:        os.Getenv("JWT_SECRET"),
		NextcloudURL:     os.Getenv("NEXTCLOUD_URL"),
		NextcloudUser:    os.Getenv("NEXTCLOUD_USER"),
		NextcloudPass:    os.Getenv("NEXTCLOUD_PASSWORD"),
		NextcloudBase:    getEnv("NEXTCLOUD_BASEPATH", "/"),
		AllowedOrigins:   getEnv("ALLOWED_ORIGINS", "https://b0esche.cloud,https://www.b0esche.cloud,https://*.b0esche.cloud,http://localhost:8080"),
	}
	fmt.Printf("[CONFIG] Nextcloud URL: %q, User: %q, BasePath: %q\n", cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudBase)
	return cfg
}

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