package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ScalingPolicy defines how the scale set should scale
type ScalingPolicy struct {
	// Type of scaling policy
	// +kubebuilder:validation:Enum=CPU;MEMORY;CUSTOM
	Type string `json:"type"`

	// Metric name for custom metrics
	MetricName string `json:"metricName,omitempty"`

	// Target value for the metric (e.g., "80" for 80% CPU)
	TargetValue string `json:"targetValue"`

	// Scale up threshold (percentage)
	ScaleUpThreshold string `json:"scaleUpThreshold,omitempty"`

	// Scale down threshold (percentage)
	ScaleDownThreshold string `json:"scaleDownThreshold,omitempty"`
}

// VMScaleSetSpec defines the desired state of ProxmoxVMScaleSet
type VMScaleSetSpec struct {
	// Template defines the VM template configuration
	Template ProxmoxVMParameters `json:"template"`

	// MinReplicas is the minimum number of replicas
	// +kubebuilder:validation:Minimum=1
	// +kubebuilder:default=1
	MinReplicas int `json:"minReplicas,omitempty"`

	// MaxReplicas is the maximum number of replicas
	// +kubebuilder:validation:Minimum=1
	// +kubebuilder:default=10
	MaxReplicas int `json:"maxReplicas,omitempty"`

	// DesiredReplicas is the desired number of replicas (for manual scaling)
	DesiredReplicas *int `json:"desiredReplicas,omitempty"`

	// ScalingPolicies defines the scaling policies
	ScalingPolicies []ScalingPolicy `json:"scalingPolicies,omitempty"`

	// CooldownPeriod is the cooldown period in seconds between scaling actions
	// +kubebuilder:default=300
	CooldownPeriod int `json:"cooldownPeriod,omitempty"`

	// ProviderConfigReference specifies how the provider should be configured
	// +kubebuilder:validation:Required
	ProviderConfigReference *ProviderConfigReference `json:"providerConfigRef"`
}

// ScalingEvent represents a scaling event
type ScalingEvent struct {
	// Type of scaling event (SCALE_UP, SCALE_DOWN)
	Type string `json:"type"`

	// Old replica count
	OldReplicas int `json:"oldReplicas"`

	// New replica count
	NewReplicas int `json:"newReplicas"`

	// Reason for scaling
	Reason string `json:"reason"`

	// Timestamp of the event
	Timestamp metav1.Time `json:"timestamp"`
}

// VMInstance represents a VM instance in the scale set
type VMInstance struct {
	// VMID is the Proxmox VM ID
	VMID int `json:"vmId"`

	// Name of the VM instance
	Name string `json:"name"`

	// State of the VM (running, stopped, etc.)
	State string `json:"state"`

	// IPAddress of the VM
	IPAddress string `json:"ipAddress,omitempty"`

	// CreatedAt timestamp
	CreatedAt metav1.Time `json:"createdAt"`
}

// VMScaleSetStatus defines the observed state of ProxmoxVMScaleSet
type VMScaleSetStatus struct {
	// CurrentReplicas is the current number of replicas
	CurrentReplicas int `json:"currentReplicas,omitempty"`

	// DesiredReplicas is the desired number of replicas
	DesiredReplicas int `json:"desiredReplicas,omitempty"`

	// ReadyReplicas is the number of ready replicas
	ReadyReplicas int `json:"readyReplicas,omitempty"`

	// Instances is the list of VM instances
	Instances []VMInstance `json:"instances,omitempty"`

	// ScalingEvents is the history of scaling events
	ScalingEvents []ScalingEvent `json:"scalingEvents,omitempty"`

	// LastScaleTime is the timestamp of the last scaling action
	LastScaleTime *metav1.Time `json:"lastScaleTime,omitempty"`

	// Conditions represent the latest available observations of the resource's state
	Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="MIN",type="integer",JSONPath=".spec.minReplicas"
// +kubebuilder:printcolumn:name="MAX",type="integer",JSONPath=".spec.maxReplicas"
// +kubebuilder:printcolumn:name="CURRENT",type="integer",JSONPath=".status.currentReplicas"
// +kubebuilder:printcolumn:name="READY",type="integer",JSONPath=".status.readyReplicas"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"

// ProxmoxVMScaleSet is the Schema for the proxmoxvmscalesets API
type ProxmoxVMScaleSet struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   VMScaleSetSpec   `json:"spec,omitempty"`
	Status VMScaleSetStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ProxmoxVMScaleSetList contains a list of ProxmoxVMScaleSet
type ProxmoxVMScaleSetList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []ProxmoxVMScaleSet `json:"items"`
}

func init() {
	SchemeBuilder.Register(&ProxmoxVMScaleSet{}, &ProxmoxVMScaleSetList{})
}

