package cluster

import (
	"testing"

	"github.com/stretchr/testify/assert"

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

func TestSetDefaultHighAvailabilityType(t *testing.T) {
	tests := []struct {
		name    string
		cluster *entity.Cluster
		want    globalngv1.Cluster_HighAvailabilityType
	}{
		{
			name: "default premium to regional",
			cluster: &entity.Cluster{Cluster: &globalngv1.Cluster{
				ServicePlan:          globalcommonv1.ServicePlan_Premium,
				HighAvailabilityType: globalngv1.Cluster_HIGH_AVAILABILITY_TYPE_UNSPECIFIED,
			}},
			want: globalngv1.Cluster_REGIONAL,
		},
		{
			name: "default essential v2 to regional",
			cluster: &entity.Cluster{Cluster: &globalngv1.Cluster{
				ServicePlan:          globalcommonv1.ServicePlan_Essential_V2,
				HighAvailabilityType: globalngv1.Cluster_HIGH_AVAILABILITY_TYPE_UNSPECIFIED,
			}},
			want: globalngv1.Cluster_REGIONAL,
		},
		{
			name: "keep explicit high availability type",
			cluster: &entity.Cluster{Cluster: &globalngv1.Cluster{
				ServicePlan:          globalcommonv1.ServicePlan_Essential_V2,
				HighAvailabilityType: globalngv1.Cluster_ZONAL,
			}},
			want: globalngv1.Cluster_ZONAL,
		},
		{
			name: "byoc remains unspecified",
			cluster: &entity.Cluster{Cluster: &globalngv1.Cluster{
				ServicePlan:          globalcommonv1.ServicePlan_BYOC,
				HighAvailabilityType: globalngv1.Cluster_HIGH_AVAILABILITY_TYPE_UNSPECIFIED,
			}},
			want: globalngv1.Cluster_HIGH_AVAILABILITY_TYPE_UNSPECIFIED,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			setDefaultHighAvailabilityType(tt.cluster)
			assert.Equal(t, tt.want, tt.cluster.GetHighAvailabilityType())
		})
	}
}
