package v1

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

const (
	SnapshotStatusCreating SnapshotStatusName = "creating"
	SnapshotStatusReady    SnapshotStatusName = "ready"
	SnapshotStatusMerging  SnapshotStatusName = "merging"
	SnapshotStatusMerged   SnapshotStatusName = "merged"
	SnapshotStatusError    SnapshotStatusName = "error"
	SnapshotStatusDeleted  SnapshotStatusName = "deleted"

	OriginVolumeNameLabelKey      = "obnmvf/origin-antstorvol-name"
	OriginVolumeNamespaceLabelKey = "obnmvf/origin-antstorvol-ns"

	// the timestamp of starting merge
	MergeStartTimestampLabelKey  = "obnmvf/merge-start-timestamp"
	MergeFinishTimestampLabelKey = "obnmvf/merge-finish-timestamp"

	// SnapUuidLabelKey is the key of snapshot's uuid. It is used for listing snapshot by label filter
	SnapUuidLabelKey = "obnvmf/snap-uuid"
)

// +kubebuilder:validation:Enum=creating;ready;merging;merged
type SnapshotStatusName string

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="size",type=integer,JSONPath=`.spec.size`
// +kubebuilder:printcolumn:name="volType",type=string,JSONPath=`.spec.volType`
// +kubebuilder:printcolumn:name="originVol",type=string,JSONPath=`.spec.originVolName`
// +kubebuilder:printcolumn:name="status",type=string,JSONPath=`.status.status`
// +kubebuilder:printcolumn:name="age",type="date",JSONPath=".metadata.creationTimestamp"
// AntstorSnapshot is the Schema for the antstorvolumes API
type AntstorSnapshot struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec AntstorSnapshotSpec `json:"spec,omitempty"`

	// +optional
	Status AntstorSnapshotStatus `json:"status,omitempty"`
}

type AntstorSnapshotSpec struct {
	// uuid is generated by controller for each snapshot
	Uuid string `json:"uuid,omitempty"`

	// VolType is KernelLVol or SpdkLVol
	// +kubebuilder:validation:Enum=KernelLVol;SpdkLVol
	VolType VolumeType `json:"volType"`

	// KernelLvol .Name indicates the name of snapshot LV. if VolType=KernelLVol, this cannot be empty
	// +optional
	KernelLvol KernelLvol `json:"kernelLvol,omitempty"`

	// +optional
	SpdkLvol SpdkLvol `json:"spdkLvol,omitempty"`

	// Size of snapshot
	Size int64 `json:"size"`

	// Name of orignal volume
	OriginVolName string `json:"originVolName"`

	// Namespace of orignal volume
	OriginVolNamespace string `json:"originVolNamespace"`

	// +optional
	OriginVolTargetNodeID string `json:"originVolTargetNodeId"`
}

type AntstorSnapshotStatus struct {
	// +optional
	Status SnapshotStatusName `json:"status"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// SnapshotList contains a list of Snapshot
type AntstorSnapshotList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []AntstorSnapshot `json:"items"`
}

func init() {
	SchemeBuilder.Register(&AntstorSnapshot{}, &AntstorSnapshotList{})
}
