package v1alpha1

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

// ProxmoxVMParameters define the desired state of a Proxmox virtual machine
type ProxmoxVMParameters struct {
	// Node is the Proxmox node to deploy the VM on
	// +kubebuilder:validation:Required
	Node string `json:"node"`

	// Name is the name of the virtual machine
	// Must be 1-100 characters, alphanumeric, hyphen, underscore, dot, or space (not at edges)
	// +kubebuilder:validation:Required
	// +kubebuilder:validation:MinLength=1
	// +kubebuilder:validation:MaxLength=100
	Name string `json:"name"`

	// CPU is the number of CPU cores
	// +kubebuilder:validation:Minimum=1
	// +kubebuilder:default=2
	CPU int `json:"cpu,omitempty"`

	// Memory is the amount of memory (e.g., "8Gi", "4096Mi")
	// Supports: Gi, Mi, Ki, G, M, K (case-insensitive) or plain numbers (assumed MB)
	// Range: 128 MB - 2 TB
	// +kubebuilder:validation:Required
	Memory string `json:"memory"`

	// Disk is the disk size (e.g., "100Gi", "50Gi")
	// Supports: Ti, Gi, Mi, T, G, M (case-insensitive) or plain numbers (assumed GB)
	// Range: 1 GB - 100 TB
	// +kubebuilder:validation:Required
	Disk string `json:"disk"`

	// Storage is the storage pool name
	// +kubebuilder:default="local-lvm"
	Storage string `json:"storage,omitempty"`

	// Network is the network bridge name (e.g., "vmbr0")
	// Must exist on the target node (validated before VM creation)
	// Format: alphanumeric, hyphen, underscore
	// +kubebuilder:default="vmbr0"
	Network string `json:"network,omitempty"`

	// Image is the OS template/image specification
	// Formats supported:
	//   - Template VMID: "100" (numeric, 100-999999999)
	//   - Volume ID: "storage:path/to/image"
	//   - Image name: "ubuntu-22.04-cloud" (max 255 chars)
	// +kubebuilder:validation:Required
	Image string `json:"image"`

	// Site is the Proxmox site identifier
	// +kubebuilder:validation:Required
	Site string `json:"site"`

	// UserData is optional cloud-init user data in YAML format
	// This will be written to the VM's cloud-init drive for first-boot configuration
	UserData string `json:"userData,omitempty"`

	// SSHKeys is a list of SSH public keys to inject
	SSHKeys []string `json:"sshKeys,omitempty"`
}

// ProxmoxVMStatus defines the observed state of ProxmoxVM
type ProxmoxVMStatus struct {
	// VMID is the Proxmox VM ID
	VMID int `json:"vmId,omitempty"`

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

	// IPAddress is the IP address of the VM
	IPAddress string `json:"ipAddress,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="VMID",type="integer",JSONPath=".status.vmId"
// +kubebuilder:printcolumn:name="STATE",type="string",JSONPath=".status.state"
// +kubebuilder:printcolumn:name="IP",type="string",JSONPath=".status.ipAddress"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"

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

	Spec   ProxmoxVMSpec   `json:"spec,omitempty"`
	Status ProxmoxVMStatus `json:"status,omitempty"`
}

// ProxmoxVMSpec defines the desired state of ProxmoxVM
type ProxmoxVMSpec struct {
	// ForProvider are the parameters for the Proxmox VM
	ForProvider ProxmoxVMParameters `json:"forProvider"`

	// ProviderConfigReference specifies how the provider that will be used
	// to create, observe, update, and delete this managed resource should
	// be configured.
	// +kubebuilder:validation:Required
	ProviderConfigReference *ProviderConfigReference `json:"providerConfigRef"`
}

// ProviderConfigReference specifies how the provider should be configured
type ProviderConfigReference struct {
	// Name of the referenced ProviderConfig
	Name string `json:"name"`
}

// +kubebuilder:object:root=true

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

func init() {
	SchemeBuilder.Register(&ProxmoxVM{}, &ProxmoxVMList{})
}

