package tracing

import (
	"testing"
)

func TestConfig_validate(t *testing.T) {
	tests := []struct {
		name    string
		config  Config
		wantErr bool
	}{
		{
			name:    "disabled",
			wantErr: false,
			config: Config{
				Enabled: false,
			},
		},
		{
			name:    "stdout",
			wantErr: false,
			config: Config{
				Enabled:    true,
				Exporter:   Stdout,
				Service:    "test",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
		{
			name:    "OTLP gRPC",
			wantErr: false,
			config: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Service:    "test",
				Endpoint:   "localhost:4717",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
		{
			name:    "unknown exporter",
			wantErr: true,
			config: Config{
				Enabled:    true,
				Exporter:   "unknown",
				Service:    "test",
				Endpoint:   "localhost:4717",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
		{
			name:    "no exporter",
			wantErr: true,
			config: Config{
				Enabled:    true,
				Service:    "test",
				Endpoint:   "localhost:4717",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
		{
			name:    "no service",
			wantErr: true,
			config: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
		{
			name:    "no endpoint for grpc",
			wantErr: true,
			config: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Service:    "test",
				InstanceID: "s01",
				Version:    "v0.0.1",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := tt.config.validate(); (err != nil) != tt.wantErr {
				t.Errorf("Config.validate() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestConfig_hasChange(t *testing.T) {
	tests := []struct {
		name   string
		config Config
		other  Config
		want   bool
	}{
		{
			name: "disabled configs always equal",
			want: false,
			config: Config{
				Enabled:    false,
				Exporter:   Stdout,
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
			other: Config{
				Enabled:    false,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
		},
		{
			name: "enabled",
			want: true,
			config: Config{
				Enabled:    false,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
			other: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
		},
		{
			name: "disabled",
			want: true,
			config: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
			other: Config{
				Enabled:    false,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
		},
		{
			name: "do not use endpoint for stdout",
			want: false,
			config: Config{
				Enabled:    true,
				Exporter:   Stdout,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
			other: Config{
				Enabled:    true,
				Exporter:   Stdout,
				Endpoint:   "otherhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
		},
		{
			name: "use endpoint for grpc",
			want: true,
			config: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Endpoint:   "localhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
			other: Config{
				Enabled:    true,
				Exporter:   OTLPgRPC,
				Endpoint:   "otherhost:4717",
				Service:    "test",
				InstanceID: "s01",
				Version:    "v1.0.0",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.config.hasChange(&tt.other); got != tt.want {
				t.Errorf("Config.equal() = %v, want %v", got, tt.want)
			}
		})
	}
}