package stt

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{
				Timeout: "30s",
			},
			wantErr: true,
		},
		{
			name: "invalid timeout",
			cfg: Config{
				URL:     "http://localhost:8178",
				Timeout: "not-a-duration",
			},
			wantErr: true,
		},
		{
			name: "valid minimal config",
			cfg: Config{
				URL: "http://localhost:8178",
			},
			wantErr: false,
		},
		{
			name: "valid config with timeout",
			cfg: Config{
				URL:     "http://localhost:8178",
				Timeout: "60s",
			},
			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:8178",
			},
			wantErr: false,
		},
		{
			name: "valid config with timeout",
			cfg: Config{
				URL:     "http://localhost:8178",
				Timeout: "45s",
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			client, err := NewClient(tt.cfg, 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")
			}
		})
	}
}
