package alicloudupbound

import (
	"testing"

	aliplv1alpha1 "github.com/crossplane-contrib/provider-upjet-alibabacloud/apis/privatelink/v1alpha1"
	xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
	"github.com/stretchr/testify/assert"

	pkgalicloud "github.com/tidbcloud/observability/agent-control-server/pkg/cloud/alicloud"
)

func TestIsVPCEndpointReusable(t *testing.T) {
	svc := "svc-test"
	vpc := "vpc-123"
	et := pkgalicloud.VpcEndpointTypeInterface

	tests := []struct {
		name         string
		mockEndpoint func() *aliplv1alpha1.VpcEndpoint
		want         bool
	}{
		{
			name: "ready and matching",
			mockEndpoint: func() *aliplv1alpha1.VpcEndpoint {
				ep := &aliplv1alpha1.VpcEndpoint{}
				ep.Status.SetConditions(xpv1.Available())
				ep.Spec.ForProvider.ServiceName = &svc
				ep.Spec.ForProvider.VPCID = &vpc
				ep.Spec.ForProvider.EndpointType = &et
				return ep
			},
			want: true,
		},
		{
			name: "deleting",
			mockEndpoint: func() *aliplv1alpha1.VpcEndpoint {
				ep := &aliplv1alpha1.VpcEndpoint{}
				ep.Status.SetConditions(xpv1.Deleting())
				ep.Spec.ForProvider.ServiceName = &svc
				ep.Spec.ForProvider.VPCID = &vpc
				ep.Spec.ForProvider.EndpointType = &et
				return ep
			},
			want: false,
		},
		{
			name: "mismatch type",
			mockEndpoint: func() *aliplv1alpha1.VpcEndpoint {
				ep := &aliplv1alpha1.VpcEndpoint{}
				ep.Status.SetConditions(xpv1.Available())
				ep.Spec.ForProvider.ServiceName = &svc
				ep.Spec.ForProvider.VPCID = &vpc
				wrongType := "Gateway"
				ep.Spec.ForProvider.EndpointType = &wrongType
				return ep
			},
			want: false,
		},
		{
			name: "mismatch service",
			mockEndpoint: func() *aliplv1alpha1.VpcEndpoint {
				ep := &aliplv1alpha1.VpcEndpoint{}
				ep.Status.SetConditions(xpv1.Available())
				wrongSvc := "svc-other"
				ep.Spec.ForProvider.ServiceName = &wrongSvc
				ep.Spec.ForProvider.VPCID = &vpc
				ep.Spec.ForProvider.EndpointType = &et
				return ep
			},
			want: false,
		},
		{
			name: "mismatch vpc",
			mockEndpoint: func() *aliplv1alpha1.VpcEndpoint {
				ep := &aliplv1alpha1.VpcEndpoint{}
				ep.Status.SetConditions(xpv1.Available())
				ep.Spec.ForProvider.ServiceName = &svc
				wrongVpc := "vpc-other"
				ep.Spec.ForProvider.VPCID = &wrongVpc
				ep.Spec.ForProvider.EndpointType = &et
				return ep
			},
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, isVPCEndpointReusable(tt.mockEndpoint(), et, svc, vpc))
		})
	}
}
