package resource

// TiDBCluster is a tidb cluster api object
type TiDBCluster struct {
	ResourceHandle `json:",inline"`
	Spec           TiDBClusterSpec     `json:"spec"`
	Status         TiDBClusterStatus   `json:"status,omitempty"`
	Qos            map[string]QosClaim `json:"qos,omitempty"`
}

// TiDBClusterComponentSpec is the component spec part of a TiDBCluster
type TiDBClusterComponentSpec struct {
	Replicas    int            `json:"replicas"`
	MaxReplicas int            `json:"maxReplicas"` // for test-infra resource reservation, only can read when create stage, don't use it directly after that
	BaseImage   string         `json:"baseImage"`
	Version     string         `json:"version"`
	Config      *GenericConfig `json:"config,omitempty"`
}

// TiDBClusterStatus is the status of a TiDBCluster
type TiDBClusterStatus struct {
	ClusterID  string `json:"clusterID"`
	Conditions []struct {
		// Type of the condition.
		Type string `json:"type"`
		// Status of the condition, one of True, False, Unknown.
		Status string `json:"status"`
		// The reason for the condition's last transition.
		Reason string `json:"reason,omitempty"`
		// A human readable message indicating details about the transition.
		Message string `json:"message,omitempty"`
	} `json:"conditions"`
	PD        TiDBClusterComponentStatus `json:"pd"`
	TiKV      TiDBClusterComponentStatus `json:"tikv"`
	TiDB      TiDBClusterComponentStatus `json:"tidb"`
	TiCDC     TiDBClusterComponentStatus `json:"ticdc"`
	TiFlash   TiDBClusterComponentStatus `json:"tiflash"`
	PostStart PostStartHandlerStatus     `json:"postStart"`
}

// TiDBClusterSpec is the spec part of a TiDBCluster
type TiDBClusterSpec struct {
	Version   string                   `json:"string"`
	PD        TiDBClusterComponentSpec `json:"pd"`
	TiKV      TiDBClusterComponentSpec `json:"tikv"`
	TiDB      TiDBClusterComponentSpec `json:"tidb"`
	TiCDC     TiDBClusterComponentSpec `json:"ticdc"`
	TiFlash   TiDBClusterComponentSpec `json:"tiflash"`
	PostStart PostStartHandler         `json:"postStart"`
}

// TiDBClusterComponentStatus represents the current status of a tidb cluster component
type TiDBClusterComponentStatus struct {
	Image  string `json:"image"`
	Phase  string `json:"phase"`
	Status struct {
		ObservedGeneration int    `json:"observedGeneration"`
		Replicas           int    `json:"replicas"`
		ReadyReplicas      int    `json:"readyReplicas"`
		CurrentReplicas    int    `json:"currentReplicas"`
		CurrentRevision    string `json:"currentRevision"`
		UpdateRevision     string `json:"updateRevision"`
	} `json:"statefulSet"`
}

// PostStartHandler is executed immediately after the cluster is created
type PostStartHandler struct {
	SQL string `json:"sql,omitempty"`
}

type SQLPostStartHandlerStatusState string

const (
	SQLPostStartHandlerStatusRunning   = SQLPostStartHandlerStatusState("running")
	SQLPostStartHandlerStatusError     = SQLPostStartHandlerStatusState("error")
	SQLPostStartHandlerStatusCompleted = SQLPostStartHandlerStatusState("completed")
)

type PostStartHandlerStatus struct {
	SQL *SQLPostStartHandlerStatus `json:"sql"`
}

type SQLPostStartHandlerStatus struct {
	State SQLPostStartHandlerStatusState `json:"state"`
}
