package cluster

import (
	"strconv"

	observabilitypingcapcomv1 "github.com/tidbcloud/observability/api/regionalserver/resourceworker/v1"
	"github.com/tidbcloud/observability/pkg/common/constants"
)

func IsSharedMode(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	return clusterv1.Annotations[constants.AnnotationShareMode] == string(constants.SHARED)
}

func IsEnhancedMode(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	return clusterv1.Annotations[constants.AnnotationShareMode] == string(constants.ENHANCED)
}

func SupportServerless(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	return IsSharedMode(clusterv1) && clusterv1.Spec.VictoriaAccount.SupportServerless
}

// / for next-gen cluster we will use the same logic as RejectInsertIngress
func RejectInsertIngress(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	return IsSharedMode(clusterv1) && clusterv1.Spec.VictoriaAccount.ProjectID > 0
}

// RejectReadIngress only used for serverless specific clusters by now
// for next-gen cluster we will use the same logic as RejectReadIngress
func RejectReadIngress(cls *Cluster, clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	isServerless := IsSharedMode(clusterv1) && clusterv1.Spec.VictoriaAccount.ProjectID > 0
	if !isServerless {
		return false
	}
	bizTypeConfig := cls.serviceInfo.BizTypeConfigs[clusterv1.Labels[constants.LabelBizType]]

	allOmit := bizTypeConfig.OmitTenancyReadIngress.AllOmit
	if allOmit {
		return true
	}

	tenantID := clusterv1.Labels[constants.LabelTenantID]
	for _, omitTenant := range bizTypeConfig.OmitTenancyReadIngress.OmitTenants {
		if tenantID == omitTenant {
			return true
		}
	}
	return false
}

// IsNextGen checks if the cluster is a next-gen cluster.
func IsNextGen(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	return clusterv1.Labels[constants.LabelNextGen] == strconv.FormatBool(true)
}

// NextGenNeedEndpointService checks if the next-gen cluster needs an endpoint service.
func NextGenNeedEndpointService(clusterv1 *observabilitypingcapcomv1.Cluster) bool {
	if IsNextGen(clusterv1) && clusterv1.Spec.Endpoint.UserARN != "" {
		return true
	}
	return false
}
