package privatelink

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	globalcommonv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/common/v1"
	globalngv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
)

func TestCheckParamsAzure(t *testing.T) {
	svc := &service{}

	t.Run("azure valid request", func(t *testing.T) {
		err := svc.checkParams(&globalngv1.PrivateLinkConnection{
			EndpointId:       "/subscriptions/sub-1/resourceGroups/rg/providers/Microsoft.Network/privateEndpoints/pe-1",
			PrivateIpAddress: "10.0.1.4",
			EndpointType:     globalngv1.PrivateLinkConnection_CUSTOMER,
		}, globalcommonv1.CloudProvider_AZURE)
		require.NoError(t, err)
	})

	t.Run("azure invalid private ip", func(t *testing.T) {
		err := svc.checkParams(&globalngv1.PrivateLinkConnection{
			EndpointId:       "/subscriptions/sub-1/resourceGroups/rg/providers/Microsoft.Network/privateEndpoints/pe-1",
			PrivateIpAddress: "not-an-ip",
			EndpointType:     globalngv1.PrivateLinkConnection_CUSTOMER,
		}, globalcommonv1.CloudProvider_AZURE)
		require.Error(t, err)
		assert.Contains(t, err.Error(), "invalid private ip address")
	})

	t.Run("azure private ip empty", func(t *testing.T) {
		err := svc.checkParams(&globalngv1.PrivateLinkConnection{
			EndpointId:       "/subscriptions/sub-1/resourceGroups/rg/providers/Microsoft.Network/privateEndpoints/pe-1",
			PrivateIpAddress: "",
			EndpointType:     globalngv1.PrivateLinkConnection_CUSTOMER,
		}, globalcommonv1.CloudProvider_AZURE)
		require.Error(t, err)
		assert.Contains(t, err.Error(), "invalid private ip address")
	})

	t.Run("azure endpoint ip empty", func(t *testing.T) {
		err := svc.checkParams(&globalngv1.PrivateLinkConnection{
			EndpointId:       "",
			PrivateIpAddress: "10.0.1.4",
			EndpointType:     globalngv1.PrivateLinkConnection_CUSTOMER,
		}, globalcommonv1.CloudProvider_AZURE)
		require.Error(t, err)
		assert.Contains(t, err.Error(), "invalid endpoint id")
	})
}
