package eks

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/config"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/controllers/privatelinkendpoint/task"
	ec2v1beta1 "github.com/upbound/provider-aws/apis/ec2/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestEndpoint_getMatchedSubnetsByEndpointZones(t *testing.T) {
	tests := []struct {
		name     string
		vpc      *v1alpha1.VPC
		zones    []string
		expected []string
		wantErr  string
	}{
		{
			name: "default uses node subnets",
			vpc: &v1alpha1.VPC{
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
					},
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-b", AvailabilityZoneID: "use1-az2"},
					},
				},
			},
			zones:    []string{"use1-az1"},
			expected: []string{"subnet-node-a"},
		},
		{
			name: "default returns all requested node subnets",
			vpc: &v1alpha1.VPC{
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
						{SubnetID: "subnet-node-b", AvailabilityZoneID: "use1-az2"},
					},
				},
			},
			zones:    []string{"use1-az1", "use1-az2"},
			expected: []string{"subnet-node-a", "subnet-node-b"},
		},
		{
			name: "configured single node zone uses private link endpoint subnets",
			vpc: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b"},
				},
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
					},
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-b", AvailabilityZoneID: "use1-az2"},
					},
				},
			},
			zones:    []string{"use1-az2"},
			expected: []string{"subnet-endpoint-b"},
		},
		{
			name: "configured single node zone returns all requested private link endpoint subnets",
			vpc: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b", "us-east-1c"},
				},
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
					},
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-a", AvailabilityZoneID: "use1-az1"},
						{SubnetID: "subnet-endpoint-b", AvailabilityZoneID: "use1-az2"},
						{SubnetID: "subnet-endpoint-c", AvailabilityZoneID: "use1-az3"},
					},
				},
			},
			zones:    []string{"use1-az1", "use1-az2", "use1-az3"},
			expected: []string{"subnet-endpoint-a", "subnet-endpoint-b", "subnet-endpoint-c"},
		},
		{
			name: "configured single node zone requires every requested endpoint subnet",
			vpc: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b", "us-east-1c"},
				},
				Status: v1alpha1.VPCStatus{
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-a", AvailabilityZoneID: "use1-az1"},
						{SubnetID: "subnet-endpoint-b", AvailabilityZoneID: "use1-az2"},
					},
				},
			},
			zones:   []string{"use1-az1", "use1-az3"},
			wantErr: "az use1-az3 is not found in private link endpoint subnets",
		},
		{
			name: "configured single node zone waits for private link endpoint subnets",
			vpc: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b"},
				},
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
					},
				},
			},
			zones:   []string{"use1-az2"},
			wantErr: "private link endpoint subnets are not ready",
		},
		{
			name: "configured multi node zone falls back to node subnets",
			vpc: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a", "us-east-1b"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b"},
				},
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZoneID: "use1-az1"},
					},
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-b", AvailabilityZoneID: "use1-az2"},
					},
				},
			},
			zones:   []string{"use1-az2"},
			wantErr: "az use1-az2 is not found in subnets",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			epTask := &endpoint{
				BaseTask: &task.BaseTask{Context: &task.Context{
					VPC: tt.vpc,
					PrivateLinkEndpoint: &v1alpha1.PrivateLinkEndpoint{
						ObjectMeta: metav1.ObjectMeta{
							Name: "test-endpoint",
						},
						Spec: v1alpha1.PrivateLinkEndpointSpec{
							Zones: tt.zones,
						},
					},
					Config: config.RootConfig{
						ControllerConfig: config.ControllerConfig{},
					},
				}},
			}

			subnets, err := epTask.getMatchedSubnetsByEndpointZones()
			if tt.wantErr != "" {
				require.Error(t, err)
				assert.Contains(t, err.Error(), tt.wantErr)
				return
			}
			require.NoError(t, err)
			assert.Equal(t, tt.expected, subnets)
		})
	}
}

func TestEndpoint_syncEndpointInfoUsesPrivateLinkEndpointSubnets(t *testing.T) {
	epTask := &endpoint{
		BaseTask: &task.BaseTask{Context: &task.Context{
			VPC: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b"},
				},
				Status: v1alpha1.VPCStatus{
					NodeSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-node-a", AvailabilityZone: "us-east-1a", AvailabilityZoneID: "use1-az1"},
					},
					PrivateLinkEndpointSubnets: []v1alpha1.Subnet{
						{SubnetID: "subnet-endpoint-b", AvailabilityZone: "us-east-1b", AvailabilityZoneID: "use1-az2"},
						{SubnetID: "subnet-endpoint-c", AvailabilityZone: "us-east-1c", AvailabilityZoneID: "use1-az3"},
					},
				},
			},
			PrivateLinkEndpoint: &v1alpha1.PrivateLinkEndpoint{
				ObjectMeta: metav1.ObjectMeta{
					Name: "endpoint-a",
				},
				Spec: v1alpha1.PrivateLinkEndpointSpec{
					Zones: []string{"use1-az2", "use1-az3"},
				},
			},
			Config: config.RootConfig{
				ControllerConfig: config.ControllerConfig{},
			},
		}},
	}
	ep := &ec2v1beta1.VPCEndpoint{
		Status: ec2v1beta1.VPCEndpointStatus{
			AtProvider: ec2v1beta1.VPCEndpointObservation_2{
				ID:    strPtr("vpce-1"),
				State: strPtr("available"),
				DNSEntry: []ec2v1beta1.DNSEntryObservation{
					{
						DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com"),
						HostedZoneID: strPtr("Z7HUB22UULQXV"),
					},
					{
						DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-east-1b.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com"),
						HostedZoneID: strPtr("Z7HUB22UULQXV"),
					},
					{
						DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-east-1c.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com"),
						HostedZoneID: strPtr("Z7HUB22UULQXV"),
					},
				},
			},
		},
	}

	err := epTask.syncEndpointInfo(context.Background(), ep)
	require.NoError(t, err)
	assert.Equal(t, v1alpha1.PrivateLinkEndpointPhaseEndpointAvailable, epTask.PrivateLinkEndpoint.Status.Phase)
	assert.Equal(t, "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com", epTask.PrivateLinkEndpoint.Status.EndpointAddress)
	assert.Equal(t, map[string]string{
		"use1-az2": "vpce-023af6250979c0365-j9m3ti32-us-east-1b.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com",
		"use1-az3": "vpce-023af6250979c0365-j9m3ti32-us-east-1c.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com",
	}, epTask.PrivateLinkEndpoint.Status.ZonesAddresses)
}

func TestEndpoint_shouldRefreshEndpointDNSFromAWSRequiresAllRequestedZones(t *testing.T) {
	epTask := &endpoint{
		BaseTask: &task.BaseTask{Context: &task.Context{
			VPC: &v1alpha1.VPC{
				Spec: v1alpha1.VPCSpec{
					NodeZones:                []string{"us-east-1a"},
					PrivateLinkEndpointZones: []string{"us-east-1a", "us-east-1b"},
				},
			},
			PrivateLinkEndpoint: &v1alpha1.PrivateLinkEndpoint{
				Spec: v1alpha1.PrivateLinkEndpointSpec{
					Zones: []string{"use1-az1", "use1-az2"},
				},
				Status: v1alpha1.PrivateLinkEndpointStatus{
					ZonesAddresses: map[string]string{
						"use1-az1": "vpce-zone-a.example.com",
					},
				},
			},
		}},
	}

	assert.True(t, epTask.shouldRefreshEndpointDNSFromAWS())

	epTask.PrivateLinkEndpoint.Status.ZonesAddresses["use1-az2"] = "vpce-zone-b.example.com"
	assert.False(t, epTask.shouldRefreshEndpointDNSFromAWS())
}

func TestParsePLEDNSEntries(t *testing.T) {
	tests := []struct {
		name                     string
		entries                  []ec2v1beta1.DNSEntryObservation
		logicalToPhysicalZoneMap map[string]string
		expectedRegionalAddress  string
		expectedZonalAddresses   map[string]string
		expectedError            string
	}{
		{
			name: "normal case with 3 AZs and private DNS",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2b.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2c.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("privatelink-19964668.vwiyyv21q7co.clusters.tidb-cloud.com"),
					HostedZoneID: strPtr("Z09814683NRQ8JG4GH6UE"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-west-2a": "usw2-az1",
				"us-west-2b": "usw2-az2",
				"us-west-2c": "usw2-az3",
			},
			expectedRegionalAddress: "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"usw2-az1": "vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
				"usw2-az2": "vpce-023af6250979c0365-j9m3ti32-us-west-2b.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
				"usw2-az3": "vpce-023af6250979c0365-j9m3ti32-us-west-2c.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name: "only private DNS name, no AWS DNS",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("privatelink-19964668.vwiyyv21q7co.clusters.tidb-cloud.com"),
					HostedZoneID: strPtr("Z09814683NRQ8JG4GH6UE"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "",
			expectedZonalAddresses:   nil,
			expectedError:            "dns entries of private link should have at least 2",
		},
		{
			name: "only regional DNS without zonal DNS",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("privatelink-19964668.vwiyyv21q7co.clusters.tidb-cloud.com"),
					HostedZoneID: strPtr("Z09814683NRQ8JG4GH6UE"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			expectedZonalAddresses:   map[string]string{},
			expectedError:            "",
		},
		{
			name: "DNS with wrong level count (5 levels)",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "",
			expectedZonalAddresses:   nil,
			expectedError:            "zonal dns name should have 6 level, but 5",
		},
		{
			name: "DNS with wrong level count (7 levels)",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.extra.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "",
			expectedZonalAddresses:   nil,
			expectedError:            "zonal dns name should have 6 level, but 7",
		},
		{
			name: "zonal DNS with wrong parts count in first level",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "",
			expectedZonalAddresses:   nil,
			expectedError:            "the sixth level of zonal dns name should have 6 parts, but 5",
		},
		{
			name: "multiple private DNS names",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("privatelink-19964668.vwiyyv21q7co.clusters.tidb-cloud.com"),
					HostedZoneID: strPtr("Z09814683NRQ8JG4GH6UE"),
				},
				{
					DNSName:      strPtr("another-custom.domain.com"),
					HostedZoneID: strPtr("Z09814683NRQ8JG4GH6UE"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-west-2a": "usw2-az1",
			},
			expectedRegionalAddress: "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"usw2-az1": "vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name: "single AZ deployment",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z7HUB22UULQXV"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-east-1a.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z7HUB22UULQXV"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-east-1a": "use1-az1",
			},
			expectedRegionalAddress: "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"use1-az1": "vpce-023af6250979c0365-j9m3ti32-us-east-1a.vpce-svc-0e427c5d93acce18f.us-east-1.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name:                     "empty entries",
			entries:                  []ec2v1beta1.DNSEntryObservation{},
			logicalToPhysicalZoneMap: map[string]string{},
			expectedRegionalAddress:  "",
			expectedZonalAddresses:   nil,
			expectedError:            "dns entries of private link should have at least 2",
		},
		{
			name: "missing logical zone in map",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-west-2b": "usw2-az2", // 不包含 us-west-2a
			},
			expectedRegionalAddress: "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"": "vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name: "mixed region zonal DNS",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-eu-west-1a.vpce-svc-0e427c5d93acce18f.eu-west-1.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-west-2a": "usw2-az1",
				"eu-west-1a": "euw1-az1",
			},
			expectedRegionalAddress: "vpce-023af6250979c0365-j9m3ti32.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"usw2-az1": "vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
				"euw1-az1": "vpce-023af6250979c0365-j9m3ti32-eu-west-1a.vpce-svc-0e427c5d93acce18f.eu-west-1.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name: "only zonal DNS without regional",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-023af6250979c0365-j9m3ti32-us-west-2b.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"us-west-2a": "usw2-az1",
				"us-west-2b": "usw2-az2",
			},
			expectedRegionalAddress: "",
			expectedZonalAddresses: map[string]string{
				"usw2-az1": "vpce-023af6250979c0365-j9m3ti32-us-west-2a.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
				"usw2-az2": "vpce-023af6250979c0365-j9m3ti32-us-west-2b.vpce-svc-0e427c5d93acce18f.us-west-2.vpce.amazonaws.com",
			},
			expectedError: "",
		},
		{
			name: "special characters in VPC endpoint ID",
			entries: []ec2v1beta1.DNSEntryObservation{
				{
					DNSName:      strPtr("vpce-0a1b2c3d4e5f6g7h8-abc123xyz.vpce-svc-9i8h7g6f5e4d3c2b1a.ap-south-1.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
				{
					DNSName:      strPtr("vpce-0a1b2c3d4e5f6g7h8-abc123xyz-ap-south-1a.vpce-svc-9i8h7g6f5e4d3c2b1a.ap-south-1.vpce.amazonaws.com"),
					HostedZoneID: strPtr("Z1YSA3EXCYUU9Z"),
				},
			},
			logicalToPhysicalZoneMap: map[string]string{
				"ap-south-1a": "aps1-az1",
			},
			expectedRegionalAddress: "vpce-0a1b2c3d4e5f6g7h8-abc123xyz.vpce-svc-9i8h7g6f5e4d3c2b1a.ap-south-1.vpce.amazonaws.com",
			expectedZonalAddresses: map[string]string{
				"aps1-az1": "vpce-0a1b2c3d4e5f6g7h8-abc123xyz-ap-south-1a.vpce-svc-9i8h7g6f5e4d3c2b1a.ap-south-1.vpce.amazonaws.com",
			},
			expectedError: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			regionalAddress, zonalAddresses, err := parsePLEDNSEntries(tt.entries, tt.logicalToPhysicalZoneMap)

			if tt.expectedError != "" {
				require.Error(t, err)
				assert.Contains(t, err.Error(), tt.expectedError)
			} else {
				require.NoError(t, err)
				assert.Equal(t, tt.expectedRegionalAddress, regionalAddress)
				assert.Equal(t, tt.expectedZonalAddresses, zonalAddresses)
			}
		})
	}
}

func strPtr(s string) *string {
	return &s
}
