package main

import (
	"os"
	"strings"
	"testing"
)

// TestConfig_LoadConfig tests configuration loading
func TestConfig_LoadConfig(t *testing.T) {
	// Save original env vars
	origARPURL := os.Getenv("ARP_URL")
	origARPUsername := os.Getenv("ARP_USERNAME")
	origARPPassword := os.Getenv("ARP_PASSWORD")
	origOpenAIKey := os.Getenv("OPENAI_API_KEY")
	origOpenAIModel := os.Getenv("OPENAI_MODEL")
	origOpenAITemp := os.Getenv("OPENAI_TEMPERATURE")
	origOpenAIBaseURL := os.Getenv("OPENAI_BASE_URL")

	// Restore after test
	defer func() {
		os.Setenv("ARP_URL", origARPURL)
		os.Setenv("ARP_USERNAME", origARPUsername)
		os.Setenv("ARP_PASSWORD", origARPPassword)
		os.Setenv("OPENAI_API_KEY", origOpenAIKey)
		os.Setenv("OPENAI_MODEL", origOpenAIModel)
		os.Setenv("OPENAI_TEMPERATURE", origOpenAITemp)
		os.Setenv("OPENAI_BASE_URL", origOpenAIBaseURL)
	}()

	t.Run("ValidConfig", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Setenv("OPENAI_API_KEY", "test-key")
		os.Setenv("OPENAI_MODEL", "gpt-4")
		os.Setenv("OPENAI_TEMPERATURE", "0.7")
		os.Setenv("OPENAI_BASE_URL", "http://localhost:11434/v1")

		cfg, err := LoadConfig()
		if err != nil {
			t.Fatalf("LoadConfig failed: %v", err)
		}

		if cfg.ARPURL != "http://localhost:8080" {
			t.Errorf("Expected ARP_URL 'http://localhost:8080', got '%s'", cfg.ARPURL)
		}
		if cfg.ARPUsername != "test@example.com" {
			t.Errorf("Expected ARP_USERNAME 'test@example.com', got '%s'", cfg.ARPUsername)
		}
		if cfg.ARPPassword != "testpass" {
			t.Errorf("Expected ARP_PASSWORD 'testpass', got '%s'", cfg.ARPPassword)
		}
		if cfg.OpenAIKey != "test-key" {
			t.Errorf("Expected OpenAIKey 'test-key', got '%s'", cfg.OpenAIKey)
		}
		if cfg.OpenAIModel != "gpt-4" {
			t.Errorf("Expected OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
		}
		if cfg.OpenAITemperature != 0.7 {
			t.Errorf("Expected OpenAITemperature 0.7, got %f", cfg.OpenAITemperature)
		}
		if cfg.OpenAIBaseURL != "http://localhost:11434/v1" {
			t.Errorf("Expected OpenAIBaseURL 'http://localhost:11434/v1', got '%s'", cfg.OpenAIBaseURL)
		}
	})

	t.Run("DefaultValues", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Setenv("OPENAI_API_KEY", "test-key")
		os.Unsetenv("OPENAI_MODEL")
		os.Unsetenv("OPENAI_TEMPERATURE")
		os.Unsetenv("OPENAI_BASE_URL")

		cfg, err := LoadConfig()
		if err != nil {
			t.Fatalf("LoadConfig failed: %v", err)
		}

		if cfg.OpenAIModel != "gpt-4" {
			t.Errorf("Expected default OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
		}
		if cfg.OpenAITemperature != 0.0 {
			t.Errorf("Expected default OpenAITemperature 0.0, got %f", cfg.OpenAITemperature)
		}
		if cfg.OpenAIBaseURL != "" {
			t.Errorf("Expected default OpenAIBaseURL '', got '%s'", cfg.OpenAIBaseURL)
		}
	})

	t.Run("MissingARPURL", func(t *testing.T) {
		os.Unsetenv("ARP_URL")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Setenv("OPENAI_API_KEY", "test-key")

		_, err := LoadConfig()
		if err == nil {
			t.Error("Expected error for missing ARP_URL")
		}
		if !strings.Contains(err.Error(), "ARP_URL") {
			t.Errorf("Expected error to mention ARP_URL, got: %v", err)
		}
	})

	t.Run("MissingARPUsername", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Unsetenv("ARP_USERNAME")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Setenv("OPENAI_API_KEY", "test-key")

		_, err := LoadConfig()
		if err == nil {
			t.Error("Expected error for missing ARP_USERNAME")
		}
		if !strings.Contains(err.Error(), "ARP_USERNAME") {
			t.Errorf("Expected error to mention ARP_USERNAME, got: %v", err)
		}
	})

	t.Run("MissingARPPassword", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Unsetenv("ARP_PASSWORD")
		os.Setenv("OPENAI_API_KEY", "test-key")

		_, err := LoadConfig()
		if err == nil {
			t.Error("Expected error for missing ARP_PASSWORD")
		}
		if !strings.Contains(err.Error(), "ARP_PASSWORD") {
			t.Errorf("Expected error to mention ARP_PASSWORD, got: %v", err)
		}
	})

	t.Run("MissingOpenAIKey", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Unsetenv("OPENAI_API_KEY")

		_, err := LoadConfig()
		if err == nil {
			t.Error("Expected error for missing OPENAI_API_KEY")
		}
		if !strings.Contains(err.Error(), "OPENAI_API_KEY") {
			t.Errorf("Expected error to mention OPENAI_API_KEY, got: %v", err)
		}
	})

	t.Run("InvalidTemperature", func(t *testing.T) {
		os.Setenv("ARP_URL", "http://localhost:8080")
		os.Setenv("ARP_USERNAME", "test@example.com")
		os.Setenv("ARP_PASSWORD", "testpass")
		os.Setenv("OPENAI_API_KEY", "test-key")
		os.Setenv("OPENAI_TEMPERATURE", "invalid")

		cfg, err := LoadConfig()
		if err != nil {
			t.Fatalf("LoadConfig failed: %v", err)
		}

		// Should use default value when parsing fails
		if cfg.OpenAITemperature != 0.0 {
			t.Errorf("Expected default OpenAITemperature 0.0 for invalid input, got %f", cfg.OpenAITemperature)
		}
	})
}
