package v1alpha1

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

type BucketPhase string

const (
	BucketPhaseCreating BucketPhase = "Creating"
	BucketPhaseReady    BucketPhase = "Ready"
	BucketPhaseDeleting BucketPhase = "Deleting"
)

// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=regionserver
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="BUCKET",type="string",JSONPath=".status.bucket.name"
// +kubebuilder:printcolumn:name="BUCKET_ID",type="string",JSONPath=".spec.id"
// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase`

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

	Spec   BucketSpec   `json:"spec,omitempty"`
	Status BucketStatus `json:"status,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="has(self.ownership) == has(oldSelf.ownership) && (!has(self.ownership) || self.ownership == oldSelf.ownership)",message="ownership is immutable"
type BucketSpec struct {
	DataplaneId         string                `json:"dataplaneId,omitempty"`
	Id                  string                `json:"id,omitempty"`
	Region              string                `json:"region,omitempty"`
	Tags                map[string]string     `json:"tags,omitempty"`
	AccountRef          *AccountReference     `json:"accountRef,omitempty"`
	NodeRoleARN         string                `json:"nodeRoleARN,omitempty"`
	GrantedDataplaneIds []string              `json:"grantedDataplaneIds,omitempty"`
	LifecycleRules      []BucketLifecycleRule `json:"lifecycleRules,omitempty"`
	// ManualBackupLifecycleRules holds lifecycle rules for manual backup deletion.
	// These rules are managed independently from LifecycleRules via incremental add/remove APIs,
	// and are merged with LifecycleRules when syncing to the cloud provider.
	ManualBackupLifecycleRules []BucketLifecycleRule `json:"manualBackupLifecycleRules,omitempty"`
	// +optional
	Ownership *ResourceOwnership `json:"ownership,omitempty"`
}

type BucketLifecycleRule struct {
	Id                             string            `json:"id,omitempty"`
	Enabled                        bool              `json:"enabled,omitempty"`
	PrefixFilter                   string            `json:"prefixFilter,omitempty"`
	TagsFilter                     []BucketTagFilter `json:"tagsFilter,omitempty"`
	ExpirationDays                 uint32            `json:"expirationDays,omitempty"`
	IncompleteUploadExpirationDays uint32            `json:"incompleteUploadExpirationDays,omitempty"`
}

type BucketTagFilter struct {
	Key   string `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

type BucketStatus struct {
	Phase        BucketPhase `json:"phase,omitempty"`
	Bucket       *Reference  `json:"bucket,omitempty"`
	BucketPolicy *Reference  `json:"bucketPolicy,omitempty"`
}

// +kubebuilder:object:root=true
type BucketList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []Bucket `json:"items"`
}

// GetAllLifecycleRules returns both regular and manual backup lifecycle rules merged into a single slice.
func (s *BucketSpec) GetAllLifecycleRules() []BucketLifecycleRule {
	return append(s.LifecycleRules, s.ManualBackupLifecycleRules...)
}

func init() {
	SchemeBuilder.Register(&Bucket{}, &BucketList{})
}
