package test

import (
	"strconv"
	"time"
)

// TestConfig implements the ConfigProvider interface for testing
type TestConfig struct {
	values map[string]string
}

func (c *TestConfig) GetString(key string) string {
	return c.values[key]
}

func (c *TestConfig) GetInt(key string) int {
	if value, exists := c.values[key]; exists {
		if intVal, err := strconv.Atoi(value); err == nil {
			return intVal
		}
	}
	return 0
}

func (c *TestConfig) GetBool(key string) bool {
	if value, exists := c.values[key]; exists {
		if boolVal, err := strconv.ParseBool(value); err == nil {
			return boolVal
		}
	}
	// Special handling for cache enabled
	if key == "CACHE_ENABLED" {
		return c.values[key] == "true"
	}
	return false
}

func (c *TestConfig) GetDuration(key string) time.Duration {
	if value, exists := c.values[key]; exists {
		if duration, err := time.ParseDuration(value); err == nil {
			return duration
		}
	}
	return 0
}

func (c *TestConfig) GetStringSlice(key string) []string {
	if value, exists := c.values[key]; exists {
		if value == "" {
			return []string{}
		}
		// Simple split by comma for testing
		return []string{value}
	}
	return []string{}
}

func (c *TestConfig) IsSet(key string) bool {
	_, exists := c.values[key]
	return exists
}

func (c *TestConfig) Validate() error {
	return nil // Skip validation for tests
}

func (c *TestConfig) GetDatabaseDSN() string {
	return "host=" + c.GetString("DB_HOST") +
		" port=" + c.GetString("DB_PORT") +
		" user=" + c.GetString("DB_USER") +
		" password=" + c.GetString("DB_PASSWORD") +
		" dbname=" + c.GetString("DB_NAME") +
		" sslmode=" + c.GetString("DB_SSLMODE")
}

func (c *TestConfig) GetServerAddress() string {
	return c.GetString("SERVER_HOST") + ":" + c.GetString("SERVER_PORT")
}

func (c *TestConfig) GetMetricsAddress() string {
	return c.GetString("SERVER_HOST") + ":9090"
}

func (c *TestConfig) IsDevelopment() bool {
	return c.GetString("APP_ENV") == "test" || c.GetString("APP_ENV") == "development"
}

func (c *TestConfig) IsProduction() bool {
	return c.GetString("APP_ENV") == "production"
}

func (c *TestConfig) GetJWTSecret() string {
	return c.GetString("JWT_SECRET")
}

// NewTestConfig creates a test configuration with default values
func NewTestConfig() *TestConfig {
	return &TestConfig{
		values: map[string]string{
			"DB_HOST":     "localhost",
			"DB_PORT":     "5432",
			"DB_USER":     "kms_user",
			"DB_PASSWORD": "kms_password",
			"DB_NAME":     "kms_db",
			"DB_SSLMODE":  "disable",
			"SERVER_HOST": "localhost",
			"SERVER_PORT": "8080",
			"APP_ENV":     "test",
			"JWT_SECRET":  "test-jwt-secret-for-testing-only",
		},
	}
}

// NewMockConfig creates a mock configuration (alias for NewTestConfig for backward compatibility)
func NewMockConfig() *TestConfig {
	return NewTestConfig()
}
