package utils

import (
	"golang.org/x/mod/semver"

	"github.com/tidbcloud/eks-provider/controllers/cluster/task"
	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

const minBootstrapSQLSupportedVersion = "v6.5.1"

func IsClusterCreating(c *cloudv1.Cluster) bool {
	if c == nil {
		return false
	}
	return c.Status.Phase == "" || c.Status.Phase == cloudv1.ClusterPhasePending || c.Status.Phase == cloudv1.ClusterPhaseCreating
}

func IsClusterSpecModifiable(c *cloudv1.Cluster) bool {
	if c == nil {
		return false
	}
	return c.Status.Phase != cloudv1.ClusterPhaseSuspend && c.Status.Phase != cloudv1.ClusterPhaseHibernated
}

func IsBootstrapSQLEnabled(ctx *task.Context, version string) bool {
	return semver.Compare(version, minBootstrapSQLSupportedVersion) >= 0
}

// GetEffectiveHostName returns the appropriate host name based on DNS availability.
// When DNS is disabled, it returns an empty string to reflect that DNS is unavailable
// while keeping LoadBalancer active. This ensures status accurately represents the
// actual connectivity state.
func GetEffectiveHostName(dnsName string, dnsDisabled bool) string {
	if dnsDisabled {
		return ""
	}
	return dnsName
}
