package common

import cloudv1 "github.com/tidbcloud/infra-api/api/v1"

// RestartPhase describes one step in the component-by-component restart sequence during a
// TiDB node upgrade. Components within the same phase are restarted concurrently (annotations
// written in one Update call); different phases execute strictly in order.
type RestartPhase struct {
	// Name is used in log messages and Kubernetes events.
	Name string
	// Components lists the TiDB operator component types to restart in this phase.
	Components []cloudv1.ComponentType
	// Reason is written to UpgradeOperation.Status.Reason while waiting for the phase to complete.
	Reason string
}

// DefaultRestartPhases defines the order in which TiDB components are restarted during a node
// upgrade. To change the upgrade order, edit only this variable — the orchestration code in
// EKS/GKE upgraders iterates over it without knowledge of the specific phases.
//
// Current order:
//  1. TiProxy  — restarted first so it stops accepting new connections before the storage layer moves.
//  2. PD + TiKV + TiFlash — storage layer restarted together once TiProxy is stable.
//  3. TiDB — SQL layer restarted last, after storage is fully ready.
var DefaultRestartPhases = []RestartPhase{
	{
		Name:       "TiProxy",
		Components: []cloudv1.ComponentType{cloudv1.ComponentTiProxy},
		Reason:     ReasonRestartingTiProxy,
	},
	{
		Name:       "Storage (PD/TiKV/TiFlash)",
		Components: []cloudv1.ComponentType{cloudv1.ComponentPD, cloudv1.ComponentTiKV, cloudv1.ComponentTiFlash},
		Reason:     ReasonRestartingStorage,
	},
	{
		Name:       "TiDB",
		Components: []cloudv1.ComponentType{cloudv1.ComponentTiDB},
		Reason:     ReasonRestartingTiDB,
	},
}
