package main

import (
	"fmt"
	"os"
	"strconv"
)

// Config holds the agent configuration
type Config struct {
	// ARP Server Configuration
	ARPURL      string
	ARPUsername string
	ARPPassword string

	// OpenAI Configuration
	OpenAIKey         string
	OpenAIModel       string
	OpenAITemperature float64
	OpenAIBaseURL     string
	OpenAIMaxTokens   int

	// Agent Identity Configuration
	AgentName      string
	Specialization string
	Values         string
	Goals          string

	// Queue Configuration
	MaxQueueSize int

	// Agent Iteration Configuration
	MaxIterations int

	// MCP Configuration
	MCPConfigPath string
}

// LoadConfig loads configuration from environment variables
func LoadConfig() (*Config, error) {
	cfg := &Config{
		ARPURL:      os.Getenv("ARP_URL"),
		ARPUsername: os.Getenv("ARP_USERNAME"),
		ARPPassword: os.Getenv("ARP_PASSWORD"),
		OpenAIKey:   os.Getenv("OPENAI_API_KEY"),
	}

	// Required fields
	if cfg.ARPURL == "" {
		return nil, fmt.Errorf("ARP_URL environment variable is required")
	}
	if cfg.ARPUsername == "" {
		return nil, fmt.Errorf("ARP_USERNAME environment variable is required")
	}
	if cfg.ARPPassword == "" {
		return nil, fmt.Errorf("ARP_PASSWORD environment variable is required")
	}
	if cfg.OpenAIKey == "" {
		return nil, fmt.Errorf("OPENAI_API_KEY environment variable is required")
	}

	// Optional fields with defaults
	cfg.OpenAIModel = getEnvWithDefault("OPENAI_MODEL", "gpt-4")
	cfg.OpenAITemperature = getEnvFloatWithDefault("OPENAI_TEMPERATURE", 0.0)
	cfg.OpenAIBaseURL = os.Getenv("OPENAI_BASE_URL") // Empty means use default OpenAI API
	cfg.OpenAIMaxTokens = getEnvIntWithDefault("OPENAI_MAX_TOKENS", 4096)

	// Agent Identity Configuration with defaults
	cfg.AgentName = getEnvWithDefault("ARP_AGENT_NAME", "AI Assistant")
	cfg.Specialization = getEnvWithDefault("ARP_AGENT_SPECIALIZATION", "general assistance")
	cfg.Values = getEnvWithDefault("ARP_AGENT_VALUES", "helpfulness, accuracy, and collaboration")
	cfg.Goals = getEnvWithDefault("ARP_AGENT_GOALS", "help teammates accomplish their goals and contribute to the team's success")

	// Queue Configuration with defaults
	cfg.MaxQueueSize = getEnvIntWithDefault("ARP_MAX_QUEUE_SIZE", 100)

	// Agent Iteration Configuration with defaults
	cfg.MaxIterations = getEnvIntWithDefault("ARP_MAX_ITERATIONS", 10)

	// MCP Configuration with default
	cfg.MCPConfigPath = getEnvWithDefault("MCP_CONFIG_PATH", "")

	return cfg, nil
}

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

// getEnvFloatWithDefault returns the environment variable as float64 or a default
func getEnvFloatWithDefault(key string, defaultValue float64) float64 {
	if value := os.Getenv(key); value != "" {
		if f, err := strconv.ParseFloat(value, 64); err == nil {
			return f
		}
	}
	return defaultValue
}

// getEnvIntWithDefault returns the environment variable as int or a default
func getEnvIntWithDefault(key string, defaultValue int) int {
	if value := os.Getenv(key); value != "" {
		if i, err := strconv.Atoi(value); err == nil {
			return i
		}
	}
	return defaultValue
}
