package config

import (
	"os"
	"testing"
	"time"
)

func TestLoadDefaults(t *testing.T) {
	// Clear any environment variables that might interfere
	envVars := []string{
		"APP_NAME", "BACKEND_PORT", "DATABASE_URL",
		"MERCADOPAGO_BASE_URL", "MARKETPLACE_COMMISSION", "JWT_SECRET", "JWT_EXPIRES_IN",
		"PASSWORD_PEPPER", "CORS_ORIGINS", "ADMIN_NAME", "ADMIN_USERNAME", "ADMIN_EMAIL", "ADMIN_PASSWORD",
	}
	origEnvs := make(map[string]string)
	for _, key := range envVars {
		origEnvs[key] = os.Getenv(key)
		os.Unsetenv(key)
	}
	defer func() {
		for key, val := range origEnvs {
			if val != "" {
				os.Setenv(key, val)
			}
		}
	}()

	cfg := Load()

	if cfg.AppName != "saveinmed-performance-core" {
		t.Errorf("expected AppName 'saveinmed-performance-core', got '%s'", cfg.AppName)
	}
	if cfg.Port != "8214" {
		t.Errorf("expected Port '8214', got '%s'", cfg.Port)
	}
	if cfg.JWTSecret != "dev-secret" {
		t.Errorf("expected JWTSecret 'dev-secret', got '%s'", cfg.JWTSecret)
	}
	if cfg.JWTExpiresIn != 24*time.Hour {
		t.Errorf("expected JWTExpiresIn 24h, got %v", cfg.JWTExpiresIn)
	}
	if cfg.MarketplaceCommission != 2.5 {
		t.Errorf("expected MarketplaceCommission 2.5, got %f", cfg.MarketplaceCommission)
	}
	if len(cfg.CORSOrigins) != 1 || cfg.CORSOrigins[0] != "*" {
		t.Errorf("expected CORSOrigins ['*'], got %v", cfg.CORSOrigins)
	}
	if cfg.AdminName != "Administrator" {
		t.Errorf("expected AdminName 'Administrator', got '%s'", cfg.AdminName)
	}
	if cfg.AdminUsername != "admin" {
		t.Errorf("expected AdminUsername 'admin', got '%s'", cfg.AdminUsername)
	}
	if cfg.AdminEmail != "admin@saveinmed.com" {
		t.Errorf("expected AdminEmail 'admin@saveinmed.com', got '%s'", cfg.AdminEmail)
	}
	if cfg.AdminPassword != "admin123" {
		t.Errorf("expected AdminPassword 'admin123', got '%s'", cfg.AdminPassword)
	}
}

func TestLoadFromEnv(t *testing.T) {
	os.Setenv("APP_NAME", "test-app")
	os.Setenv("BACKEND_PORT", "9999")
	os.Setenv("DATABASE_URL", "postgres://test:test@localhost:5432/test")
	os.Setenv("MARKETPLACE_COMMISSION", "5.0")
	os.Setenv("JWT_SECRET", "super-secret")
	os.Setenv("JWT_EXPIRES_IN", "12h")
	os.Setenv("PASSWORD_PEPPER", "pepper123")
	os.Setenv("CORS_ORIGINS", "https://example.com,https://app.example.com")
	os.Setenv("ADMIN_NAME", "CustomAdmin")
	os.Setenv("ADMIN_USERNAME", "customadmin")
	os.Setenv("ADMIN_EMAIL", "custom@example.com")
	os.Setenv("ADMIN_PASSWORD", "securepass")

	defer func() {
		os.Unsetenv("APP_NAME")
		os.Unsetenv("BACKEND_PORT")
		os.Unsetenv("DATABASE_URL")
		os.Unsetenv("MARKETPLACE_COMMISSION")
		os.Unsetenv("JWT_SECRET")
		os.Unsetenv("JWT_EXPIRES_IN")
		os.Unsetenv("PASSWORD_PEPPER")
		os.Unsetenv("CORS_ORIGINS")
		os.Unsetenv("ADMIN_NAME")
		os.Unsetenv("ADMIN_USERNAME")
		os.Unsetenv("ADMIN_EMAIL")
		os.Unsetenv("ADMIN_PASSWORD")
	}()

	cfg := Load()

	if cfg.AppName != "test-app" {
		t.Errorf("expected AppName 'test-app', got '%s'", cfg.AppName)
	}
	if cfg.Port != "9999" {
		t.Errorf("expected Port '9999', got '%s'", cfg.Port)
	}
	if cfg.DatabaseURL != "postgres://test:test@localhost:5432/test" {
		t.Errorf("expected custom DatabaseURL, got '%s'", cfg.DatabaseURL)
	}
	if cfg.MarketplaceCommission != 5.0 {
		t.Errorf("expected MarketplaceCommission 5.0, got %f", cfg.MarketplaceCommission)
	}
	if cfg.JWTSecret != "super-secret" {
		t.Errorf("expected JWTSecret 'super-secret', got '%s'", cfg.JWTSecret)
	}
	if cfg.JWTExpiresIn != 12*time.Hour {
		t.Errorf("expected JWTExpiresIn 12h, got %v", cfg.JWTExpiresIn)
	}
	if cfg.PasswordPepper != "pepper123" {
		t.Errorf("expected PasswordPepper 'pepper123', got '%s'", cfg.PasswordPepper)
	}
	if len(cfg.CORSOrigins) != 2 {
		t.Errorf("expected 2 CORS origins, got %d", len(cfg.CORSOrigins))
	}
	if cfg.AdminName != "CustomAdmin" {
		t.Errorf("expected AdminName 'CustomAdmin', got '%s'", cfg.AdminName)
	}
	if cfg.AdminUsername != "customadmin" {
		t.Errorf("expected AdminUsername 'customadmin', got '%s'", cfg.AdminUsername)
	}
	if cfg.AdminEmail != "custom@example.com" {
		t.Errorf("expected AdminEmail 'custom@example.com', got '%s'", cfg.AdminEmail)
	}
	if cfg.AdminPassword != "securepass" {
		t.Errorf("expected AdminPassword 'securepass', got '%s'", cfg.AdminPassword)
	}
}

func TestAddr(t *testing.T) {
	cfg := Config{Port: "3000"}
	expected := ":3000"
	if cfg.Addr() != expected {
		t.Errorf("expected Addr '%s', got '%s'", expected, cfg.Addr())
	}
}

func TestInvalidEnvValues(t *testing.T) {
	os.Setenv("MARKETPLACE_COMMISSION", "invalid")

	defer func() {
		os.Unsetenv("MARKETPLACE_COMMISSION")
	}()

	cfg := Load()

	// Should use defaults when values are invalid
	if cfg.MarketplaceCommission != 2.5 {
		t.Errorf("expected fallback MarketplaceCommission 2.5, got %f", cfg.MarketplaceCommission)
	}
}

func TestEmptyCORSOrigins(t *testing.T) {
	os.Setenv("CORS_ORIGINS", "")
	defer os.Unsetenv("CORS_ORIGINS")

	cfg := Load()

	if len(cfg.CORSOrigins) != 1 || cfg.CORSOrigins[0] != "*" {
		t.Errorf("expected fallback CORSOrigins ['*'], got %v", cfg.CORSOrigins)
	}
}
