package utils

import (
	commonv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/common/v1"
	nextgenv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
	mgmt "github.com/tidbcloud/tidb-management-service/api/spec/tidb_mgmt_service/v1"
)

type Cluster struct {
	cluster *mgmt.Cluster
}

func NewCluster(cluster *mgmt.Cluster) *Cluster {
	return &Cluster{
		cluster: cluster,
	}
}

func (c *Cluster) GetClusterID() uint64 {
	return c.cluster.ClusterId
}

func (c *Cluster) GetSharedPoolId() string {
	return c.cluster.GetNextGenCluster().GetSharedPoolId()
}

func (c *Cluster) GetClusterName() string {
	return c.cluster.ClusterName
}

func (c *Cluster) GetTenantID() uint64 {
	return c.cluster.OrganizationId
}

func (c *Cluster) GetProjectID() uint64 {
	return c.cluster.ProjectId
}

func (c *Cluster) GetVersion() string {
	return c.cluster.GetNextGenCluster().Version
}

func (c *Cluster) GetRegionName() string {
	return c.cluster.Region
}

func (c *Cluster) GetRegionLogicalName() string {
	return c.cluster.GetNextGenCluster().RegionLogicalName
}

func (c *Cluster) GetCloudProvider() string {
	return c.cluster.CloudProvider
}

func (c *Cluster) GetState() ClusterState {
	return stateMap[c.cluster.GetNextGenCluster().GetState()]
}

func (c *Cluster) GetServicePlan() commonv1.ServicePlan {
	return c.cluster.GetNextGenCluster().GetServicePlan()
}

type ClusterState int

const (
	Cluster_STATE_UNSPECIFIED ClusterState = 0
	Cluster_CREATING          ClusterState = 1
	Cluster_AVAILABLE         ClusterState = 2
	Cluster_MODIFYING         ClusterState = 3
	Cluster_UNAVAILABLE       ClusterState = 4
	Cluster_DELETED           ClusterState = 5
)

var stateMap = map[nextgenv1.Cluster_State]ClusterState{
	nextgenv1.Cluster_STATE_UNSPECIFIED: Cluster_STATE_UNSPECIFIED,
	nextgenv1.Cluster_CREATING:          Cluster_CREATING,
	nextgenv1.Cluster_AVAILABLE:         Cluster_AVAILABLE,
	nextgenv1.Cluster_MODIFYING:         Cluster_MODIFYING,
	nextgenv1.Cluster_UNAVAILABLE:       Cluster_UNAVAILABLE,
	nextgenv1.Cluster_DELETED:           Cluster_DELETED,
}
