package v1alpha1

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

// ContainerMomDeployment represents a deployment managed by container-mom
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type ContainerMomDeployment struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   ContainerMomDeploymentSpec   `json:"spec"`
	Status ContainerMomDeploymentStatus `json:"status"`
}

// ContainerMomDeploymentSpec defines the desired state of a ContainerMomDeployment
type ContainerMomDeploymentSpec struct {
	// Source configuration
	Source SourceSpec `json:"source"`

	// Target clusters for deployment
	Clusters []string `json:"clusters"`

	// Domain configuration
	Domain DomainSpec `json:"domain,omitempty"`

	// Resource requirements
	Resources ResourceRequirements `json:"resources"`

	// Environment variables
	Env []EnvVar `json:"env,omitempty"`

	// Billing tier
	BillingTier string `json:"billingTier"`
}

// SourceSpec defines the source code location and build configuration
type SourceSpec struct {
	// Git repository URL
	GitURL string `json:"gitUrl,omitempty"`

	// Source to Image configuration
	BuildConfig *BuildConfig `json:"buildConfig,omitempty"`

	// Or direct container image
	Image string `json:"image,omitempty"`
}

// BuildConfig defines how the source code should be built
type BuildConfig struct {
	BuilderImage string            `json:"builderImage"`
	BuildArgs    map[string]string `json:"buildArgs,omitempty"`
}

// DomainSpec defines the domain configuration for the deployment
type DomainSpec struct {
	// Custom domain or use *.container.mom
	CustomDomain string `json:"customDomain,omitempty"`
	UseWildcard  bool   `json:"useWildcard,omitempty"`
	// TLS configuration
	TLS *TLSConfig `json:"tls,omitempty"`
	// DNS provider configuration for automatic DNS management
	DNSProvider *DNSProviderConfig `json:"dnsProvider,omitempty"`
}

// TLSConfig defines TLS/SSL configuration for the domain
type TLSConfig struct {
	// Use Let's Encrypt
	AutoCert bool `json:"autoCert,omitempty"`
	// Or use existing secret
	SecretName string `json:"secretName,omitempty"`
	// Email for Let's Encrypt notifications
	Email string `json:"email,omitempty"`
}

// DNSProviderConfig defines the DNS provider configuration
type DNSProviderConfig struct {
	// DNS provider (e.g., cloudflare, route53)
	Provider string `json:"provider"`
	// Credentials secret name
	SecretName string `json:"secretName"`
}

// ResourceRequirements defines the compute resources required
type ResourceRequirements struct {
	CPU    string `json:"cpu"`
	Memory string `json:"memory"`
	// Additional resource constraints
}

// EnvVar represents an environment variable
type EnvVar struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

// ContainerMomDeploymentStatus defines the observed state of a ContainerMomDeployment
type ContainerMomDeploymentStatus struct {
	Phase          string          `json:"phase,omitempty"`
	Image          string          `json:"image,omitempty"`
	Conditions     []Condition     `json:"conditions,omitempty"`
	ClusterStatus  []ClusterStatus `json:"clusterStatus,omitempty"`
	LastUpdateTime metav1.Time     `json:"lastUpdateTime,omitempty"`
}

// Condition represents a specific condition of the deployment
type Condition struct {
	Type    string `json:"type"`
	Status  string `json:"status"`
	Reason  string `json:"reason,omitempty"`
	Message string `json:"message,omitempty"`
}

// ClusterStatus represents the status of the deployment in a cluster
type ClusterStatus struct {
	Name           string `json:"name"`
	Phase          string `json:"phase"`
	URL            string `json:"url,omitempty"`
	ReadyReplicas  int32  `json:"readyReplicas"`
	CurrentVersion string `json:"currentVersion,omitempty"`
}

// ContainerMomDeploymentList contains a list of ContainerMomDeployment resources
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type ContainerMomDeploymentList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`

	Items []ContainerMomDeployment `json:"items"`
}

// DeepCopyInto copies all properties of this object into another object of the same type
func (in *Customer) DeepCopyInto(out *Customer) {
	*out = *in
	out.TypeMeta = in.TypeMeta
	in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
	in.Spec.DeepCopyInto(&out.Spec)
	in.Status.DeepCopyInto(&out.Status)
}

// DeepCopy creates a deep copy of Customer
func (in *Customer) DeepCopy() *Customer {
	if in == nil {
		return nil
	}
	out := new(Customer)
	in.DeepCopyInto(out)
	return out
}

// DeepCopyObject returns a deep copy of the Customer as a runtime.Object
func (in *Customer) DeepCopyObject() runtime.Object {
	if c := in.DeepCopy(); c != nil {
		return c
	}
	return nil
}

// DeepCopyInto copies all properties of this object into another object of the same type
func (in *CustomerList) DeepCopyInto(out *CustomerList) {
	*out = *in
	out.TypeMeta = in.TypeMeta
	in.ListMeta.DeepCopyInto(&out.ListMeta)
	if in.Items != nil {
		in, out := &in.Items, &out.Items
		*out = make([]Customer, len(*in))
		for i := range *in {
			(*in)[i].DeepCopyInto(&(*out)[i])
		}
	}
}

// DeepCopy creates a deep copy of CustomerList
func (in *CustomerList) DeepCopy() *CustomerList {
	if in == nil {
		return nil
	}
	out := new(CustomerList)
	in.DeepCopyInto(out)
	return out
}

// DeepCopyObject returns a deep copy of the CustomerList as a runtime.Object
func (in *CustomerList) DeepCopyObject() runtime.Object {
	if c := in.DeepCopy(); c != nil {
		return c
	}
	return nil
}

// DeepCopyInto copies all properties of this object into another object of the same type
func (in *ContainerMomDeployment) DeepCopyInto(out *ContainerMomDeployment) {
	*out = *in
	out.TypeMeta = in.TypeMeta
	in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
	in.Spec.DeepCopyInto(&out.Spec)
	in.Status.DeepCopyInto(&out.Status)
}

// DeepCopy creates a deep copy of ContainerMomDeployment
func (in *ContainerMomDeployment) DeepCopy() *ContainerMomDeployment {
	if in == nil {
		return nil
	}
	out := new(ContainerMomDeployment)
	in.DeepCopyInto(out)
	return out
}

// DeepCopyObject returns a deep copy of the ContainerMomDeployment as a runtime.Object
func (in *ContainerMomDeployment) DeepCopyObject() runtime.Object {
	if c := in.DeepCopy(); c != nil {
		return c
	}
	return nil
}

// DeepCopyInto copies all properties of this object into another object of the same type
func (in *ContainerMomDeploymentList) DeepCopyInto(out *ContainerMomDeploymentList) {
	*out = *in
	out.TypeMeta = in.TypeMeta
	in.ListMeta.DeepCopyInto(&out.ListMeta)
	if in.Items != nil {
		in, out := &in.Items, &out.Items
		*out = make([]ContainerMomDeployment, len(*in))
		for i := range *in {
			(*in)[i].DeepCopyInto(&(*out)[i])
		}
	}
}

// DeepCopy creates a deep copy of ContainerMomDeploymentList
func (in *ContainerMomDeploymentList) DeepCopy() *ContainerMomDeploymentList {
	if in == nil {
		return nil
	}
	out := new(ContainerMomDeploymentList)
	in.DeepCopyInto(out)
	return out
}

// DeepCopyObject returns a deep copy of the ContainerMomDeploymentList as a runtime.Object
func (in *ContainerMomDeploymentList) DeepCopyObject() runtime.Object {
	if c := in.DeepCopy(); c != nil {
		return c
	}
	return nil
}

// DeepCopyInto for CustomerSpec
func (in *CustomerSpec) DeepCopyInto(out *CustomerSpec) {
	*out = *in
	out.ResourceQuota = in.ResourceQuota
}

// DeepCopyInto for CustomerStatus
func (in *CustomerStatus) DeepCopyInto(out *CustomerStatus) {
	*out = *in
	out.UsedResources = in.UsedResources
	if in.ActiveDeployments != nil {
		in, out := &in.ActiveDeployments, &out.ActiveDeployments
		*out = make([]string, len(*in))
		copy(*out, *in)
	}
	if in.SubscriptionItems != nil {
		in, out := &in.SubscriptionItems, &out.SubscriptionItems
		*out = make(map[string]string, len(*in))
		for key, val := range *in {
			(*out)[key] = val
		}
	}
}

// DeepCopyInto for ContainerMomDeploymentSpec
func (in *ContainerMomDeploymentSpec) DeepCopyInto(out *ContainerMomDeploymentSpec) {
	*out = *in
	out.Source = in.Source
	if in.Clusters != nil {
		in, out := &in.Clusters, &out.Clusters
		*out = make([]string, len(*in))
		copy(*out, *in)
	}
	in.Domain.DeepCopyInto(&out.Domain)
	out.Resources = in.Resources
	if in.Env != nil {
		in, out := &in.Env, &out.Env
		*out = make([]EnvVar, len(*in))
		copy(*out, *in)
	}
}

// DeepCopyInto for ContainerMomDeploymentStatus
func (in *ContainerMomDeploymentStatus) DeepCopyInto(out *ContainerMomDeploymentStatus) {
	*out = *in
	if in.Conditions != nil {
		in, out := &in.Conditions, &out.Conditions
		*out = make([]Condition, len(*in))
		copy(*out, *in)
	}
	if in.ClusterStatus != nil {
		in, out := &in.ClusterStatus, &out.ClusterStatus
		*out = make([]ClusterStatus, len(*in))
		copy(*out, *in)
	}
}

// DeepCopyInto for DomainSpec
func (in *DomainSpec) DeepCopyInto(out *DomainSpec) {
	*out = *in
	if in.TLS != nil {
		in, out := &in.TLS, &out.TLS
		*out = new(TLSConfig)
		**out = **in
	}
	if in.DNSProvider != nil {
		in, out := &in.DNSProvider, &out.DNSProvider
		*out = new(DNSProviderConfig)
		**out = **in
	}
}

// DeepCopyInto for SourceSpec
func (in *SourceSpec) DeepCopyInto(out *SourceSpec) {
	*out = *in
	if in.BuildConfig != nil {
		in, out := &in.BuildConfig, &out.BuildConfig
		*out = new(BuildConfig)
		(*in).DeepCopyInto(*out)
	}
}

// DeepCopyInto for BuildConfig
func (in *BuildConfig) DeepCopyInto(out *BuildConfig) {
	*out = *in
	if in.BuildArgs != nil {
		in, out := &in.BuildArgs, &out.BuildArgs
		*out = make(map[string]string, len(*in))
		for key, val := range *in {
			(*out)[key] = val
		}
	}
}

// BillingStatus represents the billing status of a customer
type BillingStatus string

const (
	BillingStatusActive   BillingStatus = "Active"
	BillingStatusInactive BillingStatus = "Inactive"
	BillingStatusPending  BillingStatus = "Pending"
)
