package cloudconfig

type (
	ParameterApplyPolicy string
	ParameterSource      string
	RiskLevel            string
)

const (
	ParameterCustom        ParameterSource = "custom"
	ParameterTemplate      ParameterSource = "template"
	ParameterKernelDefault ParameterSource = "kernel default"

	ParameterApplyPolicyNone             ParameterApplyPolicy = "None"
	ParameterApplyPolicyHotReload        ParameterApplyPolicy = "HotReload"
	ParameterApplyPolicyRestart          ParameterApplyPolicy = "RollingRestart"
	ParameterApplyPolicyEnsureUpdateToTC ParameterApplyPolicy = "EnsureUpdateToTC"
	ParameterApplyPolicyNewInstancesOnly ParameterApplyPolicy = "NewInstancesOnly" // only used by dm currently
	ParameterApplyPolicyForceRestart     ParameterApplyPolicy = "ForceRestart"

	RiskLevelLow  RiskLevel = "low"
	RiskLevelHigh RiskLevel = "high"
)

var (
	ParameterApplyPolicyValues = []ParameterApplyPolicy{
		ParameterApplyPolicyNone,
		ParameterApplyPolicyHotReload,
		ParameterApplyPolicyRestart,
		ParameterApplyPolicyEnsureUpdateToTC,
		ParameterApplyPolicyNewInstancesOnly,
		ParameterApplyPolicyForceRestart,
	}
)

type ParameterAttribute struct {
	ClusterType  ClusterType          `json:"cluster_type"`
	Name         string               `json:"name"`
	ConfigType   string               `json:"config_type"`
	Version      string               `json:"version"`
	Type         string               `json:"type"`
	ApplyPolicy  ParameterApplyPolicy `json:"apply_policy"`
	DefaultValue any                  `json:"default_value"`
	Description  *string              `json:"description"`
	Validation   string               `json:"validation"`
	RiskLevel    RiskLevel            `json:"risk_level"` // "low" or "high"
	UpdatedBy    string               `json:"updated_by"`
}

type ParameterValue struct {
	// Value is the value of this parameter
	Value any `json:"value"`
	// Comment is the comment of this parameter
	Comment string `json:"comment"`
}

// NamedParameter is a parameter with its name.
type NamedParameter struct {
	ParameterValue `json:",inline"`

	// Name is the name of this parameter
	Name string `json:"name"`
}

// UsedParameter is a parameter in the actual configuration.
// It may be custom param or template param.
type UsedParameter struct {
	NamedParameter `json:",inline"`

	// Source is the value source of this parameter
	Source ParameterSource `json:"source"`
}

// UnifiedParameter is a parameter with attribute in the cluster config.
//
// It contains custom configs, template configs and engine configs(default configs).
// The value of a config is decided by the following sequence:
//   - value from custom params
//   - value from template params
//   - defaule value from engine
type UnifiedParameter struct {
	UsedParameter `json:",inline"`

	// Custom is the custom config value
	Custom *ParameterValue `json:"custom,omitempty"`
	// Template is the template config value
	Template *ParameterValue `json:"template,omitempty"`
	// Attribute is the attribute of this config
	// It may be nil if the parameter is not defined in the param dict.
	Attribute *ParameterAttribute `json:"attribute,omitempty"`
}
