package llm

import (
	"testing"
)

func TestConfigValidate(t *testing.T) {
	tests := []struct {
		name    string
		cfg     Config
		wantErr bool
	}{
		{
			name:    "empty config",
			cfg:     Config{},
			wantErr: true,
		},
		{
			name:    "missing URL",
			cfg:     Config{},
			wantErr: true,
		},
		{
			name: "invalid timeout",
			cfg: Config{
				URL:     "http://localhost:8080",
				Timeout: "not-a-duration",
			},
			wantErr: true,
		},
		{
			name: "valid minimal config",
			cfg: Config{
				URL: "http://localhost:8080",
			},
			wantErr: false,
		},
		{
			name: "valid config with timeout",
			cfg: Config{
				URL:     "http://localhost:8080",
				Timeout: "15m",
			},
			wantErr: false,
		},
		{
			name: "valid config with complex timeout",
			cfg: Config{
				URL:     "http://localhost:8080",
				Timeout: "1h30m45s",
			},
			wantErr: false,
		},
		{
			name: "valid full config",
			cfg: Config{
				Key:           "sk-test-key",
				SystemMessage: "You are a helpful assistant.",
				Timeout:       "30m",
				URL:           "http://localhost:8080",
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := tt.cfg.Validate()
			if (err != nil) != tt.wantErr {
				t.Errorf(
					"Validate() error = %v, wantErr %v",
					err, tt.wantErr,
				)
			}
		})
	}
}

func TestNewClient(t *testing.T) {
	tests := []struct {
		name    string
		cfg     Config
		wantErr bool
	}{
		{
			name:    "invalid config",
			cfg:     Config{},
			wantErr: true,
		},
		{
			name: "valid config without timeout",
			cfg: Config{
				URL: "http://localhost:8080",
			},
			wantErr: false,
		},
		{
			name: "valid config with timeout",
			cfg: Config{
				URL:     "http://localhost:8080",
				Timeout: "10m",
			},
			wantErr: false,
		},
		{
			name: "valid config with all fields",
			cfg: Config{
				Key:           "test-key",
				SystemMessage: "Test message",
				Timeout:       "5m",
				URL:           "http://localhost:8080",
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			client, err := NewClient(tt.cfg, nil, nil)
			if (err != nil) != tt.wantErr {
				t.Errorf(
					"NewClient() error = %v, wantErr %v",
					err, tt.wantErr,
				)
				return
			}
			if !tt.wantErr && client == nil {
				t.Error("NewClient() returned nil client")
			}
		})
	}
}
