package common

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"
	openapicommonv1beta1 "github.com/tidbcloud/tidb-management-service/api/spec/tidb_cloud_open_api/common/v1beta1"
)

func TestCvtGlobalCmekToOpenapiIncludesAWSExternalID(t *testing.T) {
	cmek, err := CvtGlobalCmekToOpenapi(&globalngv1.CustomerManagedEncryptionKey{
		CustomerManagedEncryptionKeyId: 123,
		CloudProviderId:                globalcommonv1.CloudProvider_AWS,
		RegionId:                       globalcommonv1.Region_AWS_US_WEST_2,
		ServicePlan:                    globalcommonv1.ServicePlan_Premium,
		Key: &globalngv1.CustomerManagedEncryptionKey_AwsKms_{
			AwsKms: &globalngv1.CustomerManagedEncryptionKey_AwsKms{
				KmsKeyArn: "arn:aws:kms:us-west-2:123456789012:key/test-key",
			},
		},
		Principal: &globalngv1.CustomerManagedEncryptionKey_AwsPrincipal{
			AwsPrincipal: &globalngv1.CmekAccessIamPrincipal_AwsCmekPrincipal{
				AccountId:  "123456789012",
				ExternalId: "60205",
			},
		},
	})

	require.Nil(t, err)
	assert.Equal(t, "123", cmek.GetCustomerManagedEncryptionKeyId())
	assert.Equal(t, openapicommonv1beta1.Region_aws, cmek.GetCloudProvider())
	assert.Equal(t, "123456789012", cmek.GetAwsPrincipal().GetAccountId())
	assert.Equal(t, "60205", cmek.GetAwsPrincipal().GetExternalId())
}

func TestCvtGlobalDualLayerDataEncryptionToOpenapiIncludesAWSExternalID(t *testing.T) {
	encryption, err := CvtGlobalDualLayerDataEncryptionToOpenapi(&globalngv1.Cluster_DualLayerDataEncryption{
		Key: &globalngv1.Cluster_DualLayerDataEncryption_Cmek{
			Cmek: &globalngv1.CustomerManagedEncryptionKey{
				CustomerManagedEncryptionKeyId: 123,
				CloudProviderId:                globalcommonv1.CloudProvider_AWS,
				RegionId:                       globalcommonv1.Region_AWS_US_WEST_2,
				ServicePlan:                    globalcommonv1.ServicePlan_Premium,
				Key: &globalngv1.CustomerManagedEncryptionKey_AwsKms_{
					AwsKms: &globalngv1.CustomerManagedEncryptionKey_AwsKms{
						KmsKeyArn: "arn:aws:kms:us-west-2:123456789012:key/test-key",
					},
				},
				Principal: &globalngv1.CustomerManagedEncryptionKey_AwsPrincipal{
					AwsPrincipal: &globalngv1.CmekAccessIamPrincipal_AwsCmekPrincipal{
						AccountId:  "123456789012",
						ExternalId: "60205",
					},
				},
			},
		},
	})

	require.Nil(t, err)
	require.NotNil(t, encryption.GetCmek())
	assert.Equal(t, "60205", encryption.GetCmek().GetAwsPrincipal().GetExternalId())
}

func TestCvtGlobalCmekToOpenapiIncludesAliyunAccountID(t *testing.T) {
	cmek, err := CvtGlobalCmekToOpenapi(&globalngv1.CustomerManagedEncryptionKey{
		CustomerManagedEncryptionKeyId: 456,
		CloudProviderId:                globalcommonv1.CloudProvider_ALICLOUD,
		RegionId:                       globalcommonv1.Region_ALICLOUD_AP_SOUTHEAST_1,
		ServicePlan:                    globalcommonv1.ServicePlan_Premium,
		Key: &globalngv1.CustomerManagedEncryptionKey_AliyunKms_{
			AliyunKms: &globalngv1.CustomerManagedEncryptionKey_AliyunKms{
				KmsKeyArn: "acs:kms:ap-southeast-1:5753857577068077:key/key-abc-123",
			},
		},
		Principal: &globalngv1.CustomerManagedEncryptionKey_AliyunPrincipal{
			AliyunPrincipal: &globalngv1.CmekAccessIamPrincipal_AliyunCmekPrincipal{
				AccountId: "5753857577068077",
			},
		},
	})

	require.Nil(t, err)
	assert.Equal(t, "456", cmek.GetCustomerManagedEncryptionKeyId())
	assert.Equal(t, "5753857577068077", cmek.GetAliyunPrincipal().GetAccountId())
}
