package config

import (
	"os"
)

// Config holds all application configuration
type Config struct {
	Server   ServerConfig
	Database DatabaseConfig
	JWT      JWTConfig
	Security SecurityConfig
	App      AppConfig
	Minio    MinioConfig
}

// AppConfig holds application-level settings
type AppConfig struct {
	Environment  string // "development" or "production"
	BaseDomain   string // "localhost" or "aggios.app"
}

// ServerConfig holds server-specific configuration
type ServerConfig struct {
	Port string
}

// DatabaseConfig holds database connection settings
type DatabaseConfig struct {
	Host     string
	Port     string
	User     string
	Password string
	Name     string
}

// JWTConfig holds JWT configuration
type JWTConfig struct {
	Secret string
}

// SecurityConfig holds security settings
type SecurityConfig struct {
	AllowedOrigins      []string
	MaxAttemptsPerMin   int
	PasswordMinLength   int
}

// MinioConfig holds MinIO configuration
type MinioConfig struct {
	Endpoint     string
	PublicURL    string // URL pública para acesso ao MinIO (para gerar links)
	RootUser     string
	RootPassword string
	UseSSL       bool
	BucketName   string
}

// Load loads configuration from environment variables
func Load() *Config {
	env := getEnvOrDefault("APP_ENV", "development")
	baseDomain := "localhost"
	if env == "production" {
		baseDomain = "aggios.app"
	}

	// Rate limit: more lenient in dev, strict in prod
	maxAttempts := 1000 // Aumentado drasticamente para evitar 429 durante debug
	if env == "production" {
		maxAttempts = 100 // Mais restritivo em produção
	}

	return &Config{
		Server: ServerConfig{
			Port: getEnvOrDefault("SERVER_PORT", "8080"),
		},
		Database: DatabaseConfig{
			Host:     getEnvOrDefault("DB_HOST", "localhost"),
			Port:     getEnvOrDefault("DB_PORT", "5432"),
			User:     getEnvOrDefault("DB_USER", "postgres"),
			Password: getEnvOrDefault("DB_PASSWORD", "postgres"),
			Name:     getEnvOrDefault("DB_NAME", "aggios"),
		},
		JWT: JWTConfig{
			Secret: getEnvOrDefault("JWT_SECRET", "INSECURE-fallback-secret-CHANGE-THIS"),
		},
		App: AppConfig{
			Environment: env,
			BaseDomain:  baseDomain,
		},
		Security: SecurityConfig{
			AllowedOrigins: []string{
				"http://localhost",
				"http://dash.localhost",
				"http://aggios.local",
				"http://dash.aggios.local",
				"https://aggios.app",
				"https://dash.aggios.app",
				"https://www.aggios.app",
			},
			MaxAttemptsPerMin: maxAttempts,
			PasswordMinLength: 8,
		},
		Minio: MinioConfig{
			Endpoint:     getEnvOrDefault("MINIO_ENDPOINT", "minio:9000"),
			PublicURL:    getEnvOrDefault("MINIO_PUBLIC_URL", "http://localhost:9000"),
			RootUser:     getEnvOrDefault("MINIO_ROOT_USER", "minioadmin"),
			RootPassword: getEnvOrDefault("MINIO_ROOT_PASSWORD", "changeme"),
			UseSSL:       getEnvOrDefault("MINIO_USE_SSL", "false") == "true",
			BucketName:   getEnvOrDefault("MINIO_BUCKET_NAME", "aggios"),
		},
	}
}

// getEnvOrDefault returns environment variable or default value
func getEnvOrDefault(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}
