package gke

import (
	"testing"

	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/controllers/privatelinkservice/task"
)

func Test_getEndpointName(t *testing.T) {
	cases := []struct {
		name     string
		endpoint string
		want     string
	}{
		{
			name:     "normal",
			endpoint: "projects/gcp-autonomous-dev/regions/asia-east2/forwardingRules/test",
			want:     "test",
		},
		{
			name:     "empty",
			want:     "",
			endpoint: "",
		},
	}
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			got := getEndpointName(tt.endpoint)
			if got != tt.want {
				t.Errorf("getEndpointName() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_getEndpointHost(t *testing.T) {
	cases := []struct {
		name         string
		endpoint     string
		dnsIdentify  string
		publicDomain string
		want         string
	}{
		{
			name:         "normal",
			endpoint:     "projects/gcp-autonomous-dev/regions/asia-east2/forwardingRules/test",
			dnsIdentify:  "dns1",
			publicDomain: "tidbcloud.com",
			want:         "test.dns1.tidbcloud.com",
		},
		{
			name:         "invalid endpoint",
			endpoint:     "http://",
			dnsIdentify:  "dns1",
			publicDomain: "tidbcloud.com",
			want:         "dns1.tidbcloud.com",
		},
		{
			name:         "empty",
			endpoint:     "",
			dnsIdentify:  "dns1",
			publicDomain: "tidbcloud.com",
			want:         "dns1.tidbcloud.com",
		},
	}

	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			task := NewServiceAttachment(&task.Context{
				PrivateLinkService: &v1alpha1.PrivateLinkService{
					Status: v1alpha1.PrivateLinkServiceStatus{
						DNSIdentify: tt.dnsIdentify,
					},
				}}, nil)
			task.Config.PublicHostedZoneDomain = tt.publicDomain
			got := task.getEndpointHost(tt.endpoint)
			if got != tt.want {
				t.Errorf("getEndpointHost() = %v, want %v", got, tt.want)
			}
		})
	}
}
