package resource

import validation "github.com/Thearas/ozzo-validation/v4"

type TiDBCloudCluster struct {
	ResourceHandle `json:",inline"`
	Spec           TiDBCloudClusterSpec   `json:"spec"`
	Status         TiDBCloudClusterStatus `json:"status"`
}

func (t TiDBCloudCluster) Validate() error {
	return validation.ValidateStruct(&t,
		validation.Field(&t.ResourceHandle),
		validation.Field(&t.Spec),
	)
}

type TiDBCloudClusterProvider string

const (
	TiDBCloudClusterProviderAWS = TiDBCloudClusterProvider("aws")
)

type TiDBCloudClusterTiDBSpec struct {
	Replicas        int32  `json:"replicas"`
	NodeProfileName string `json:"nodeProfileName"`
}

type TiDBCloudClusterTiKVSpec struct {
	Replicas        int32  `json:"replicas"`
	NodeProfileName string `json:"nodeProfileName"`
	StorageSizeGi   int32  `json:"storageSizeGi"`
}

type TiDBCloudClusterConnect string

const (
	TiDBCloudClusterConnectVPCPeering = TiDBCloudClusterConnect("vpc-peering")
	TiDBCloudClusterConnectStandard   = TiDBCloudClusterConnect("standard")
)

type TiDBCloudClusterSpec struct {
	AccountEmail  string                     `json:"accountEmail"`
	OrgID         string                     `json:"orgID"`
	ProjectID     string                     `json:"projectID"`
	Provider      TiDBCloudClusterProvider   `json:"provider"`
	Password      string                     `json:"password"`
	Connect       TiDBCloudClusterConnect    `json:"connect"`
	TiDB          TiDBCloudClusterTiDBSpec   `json:"tidb"`
	TiKV          TiDBCloudClusterTiKVSpec   `json:"tikv"`
	ClusterConfig TiDBCloudClusterConfigSpec `json:"clusterConfig,omitempty"`
}

func (t TiDBCloudClusterSpec) Validate() error {
	return validation.ValidateStruct(&t,
		validation.Field(&t.AccountEmail, validation.Required),
		validation.Field(&t.OrgID, validation.Required),
		validation.Field(&t.ProjectID, validation.Required),
		validation.Field(&t.Provider, validation.Required),
		validation.Field(&t.Password, validation.Required),
		validation.Field(&t.Connect, validation.Required),
	)
}

type TiDBCloudClusterState string

const (
	TiDBCloudClusterStateNormal = TiDBCloudClusterState("NORMAL")
)

type TiDBCloudClusterEndpoint struct {
	Address string `json:"address"`
	Port    int32  `json:"port"`
	Ready   bool   `json:"ready"`
}

type TiDBCloudClusterStatus struct {
	State           TiDBCloudClusterState    `json:"state"`
	ClusterID       string                   `json:"clusterId"`
	Version         string                   `json:"version"`
	PrivateEndpoint TiDBCloudClusterEndpoint `json:"privateEndpoint"`
	Endpoint        TiDBCloudClusterEndpoint `json:"endpoint"`
}

type TrafficFilter struct {
	CIDR        string `json:"cidr"`
	Description string `json:"description"`
}

// copy from https://github.com/tidbcloud/config-service/blob/09cd7b74280d6f2892f6a552b2b4cc50760c3f1f/api/v1alpha1/clusterconfig_types.go#L27

const (
	TiDBCloudBaseClusterConfig = "base"
	TiDBCloudTenentLabel       = "tidbcloud.com/tenant"
	TiDBCloudClusterLabel      = "tidbcloud.com/cluster"
)

// TiDBCloudClusterConfigSpec defines the desired state of ClusterConfig
type TiDBCloudClusterConfigSpec struct {
	// TiDB cluster version
	Version string `json:"version"`

	// TiDB contains all default and overlay configurations of tidb server
	TiDB *TiDBCloudConfiguration `json:"tidb,omitempty"`

	// PD contains all default and overlay configurations of pd server
	PD *TiDBCloudConfiguration `json:"pd,omitempty"`

	// TiKV contains all default and overlay configurations of tikv server
	TiKV *TiDBCloudConfiguration `json:"tikv,omitempty"`

	// TiFlash contains all default and overlay configurations of tiflash server
	TiFlash *TiDBCloudConfiguration `json:"tiflash,omitempty"`

	// TiFlash contains all default and overlay configurations of tiflash learner
	// https://docs.pingcap.com/tidb/dev/tiflash-configuration#configure-the-tiflash-learnertoml-file
	TiFlashLearner *TiDBCloudConfiguration `json:"tiflashLearner,omitempty"`

	// Backup contains all configurations about backup job
	Backup *TiDBCloudConfiguration `json:"backup,omitempty"`

	// Restore contains all configurations about restore job
	Restore *TiDBCloudConfiguration `json:"restore,omitempty"`
}

type TiDBCloudConfiguration struct {
	// BaseImage of the component
	BaseImage string `json:"baseImage"`

	// Tag is component image tag, if not specipy, will use global version value.
	// +optional
	Tag *string `json:"tag,omitempty"`

	// Config is the default configuration of component
	// +required
	Config *GenericConfig `json:"config"`
}
