package config

import (
	"os"
	"strconv"
)

// SearchConfig holds Elasticsearch/OpenSearch configuration
type SearchConfig struct {
	URL         string
	Username    string
	Password    string
	UseSSL      bool
	IndexPrefix string
}

// LoadSearchConfig loads search configuration from environment variables
func LoadSearchConfig() *SearchConfig {
	useSSL, _ := strconv.ParseBool(getEnv("SEARCH_USE_SSL", "false"))

	return &SearchConfig{
		URL:         getEnv("SEARCH_URL", "http://localhost:9200"),
		Username:    getEnv("SEARCH_USERNAME", ""),
		Password:    getEnv("SEARCH_PASSWORD", ""),
		UseSSL:      useSSL,
		IndexPrefix: getEnv("SEARCH_INDEX_PREFIX", "explorer"),
	}
}

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