package dto

// CreateInstanceRequest 创建实例请求
type CreateInstanceRequest struct {
	Name          string                 `json:"name" binding:"required"`
	Namespace     string                 `json:"namespace" binding:"required"`
	RegistryID    string                 `json:"registryId" binding:"required"`
	RegistryIDAlt string                 `json:"registry_id"`
	Repository    string                 `json:"repository" binding:"required"`
	Tag           string                 `json:"tag" binding:"required"`
	Description   string                 `json:"description"`
	Values        map[string]interface{} `json:"values"`
	ValuesYAML    string                 `json:"valuesYaml"`
	ValuesYAMLAlt string                 `json:"values_yaml"`
}

// UpdateInstanceRequest 更新实例请求
type UpdateInstanceRequest struct {
	Version       string                 `json:"version"`
	Description   string                 `json:"description"`
	Values        map[string]interface{} `json:"values"`
	ValuesYAML    string                 `json:"valuesYaml"`
	ValuesYAMLAlt string                 `json:"values_yaml"`
}

// Normalize 将多种命名风格的字段合并到统一字段
func (r *CreateInstanceRequest) Normalize() {
	if r.RegistryID == "" {
		r.RegistryID = r.RegistryIDAlt
	}
	if r.ValuesYAML == "" {
		r.ValuesYAML = r.ValuesYAMLAlt
	}
}

// Normalize 将多种命名风格的字段合并到统一字段
func (r *UpdateInstanceRequest) Normalize() {
	if r.ValuesYAML == "" {
		r.ValuesYAML = r.ValuesYAMLAlt
	}
}

// RollbackInstanceRequest 回滚实例请求
type RollbackInstanceRequest struct {
	Revision int  `json:"revision" binding:"required"`
	Wait     bool `json:"wait"`
	Timeout  int  `json:"timeout"` // seconds
}

// DeleteInstanceRequest 删除实例请求
type DeleteInstanceRequest struct {
	KeepHistory bool `json:"keepHistory"`
	Timeout     int  `json:"timeout"` // seconds
}

// InstanceResponse 实例响应
type InstanceResponse struct {
	ID             string                 `json:"id"`
	ClusterID      string                 `json:"clusterId"`
	Name           string                 `json:"name"`
	Namespace      string                 `json:"namespace"`
	RegistryID     string                 `json:"registryId"`
	Repository     string                 `json:"repository"`
	Chart          string                 `json:"chart"`
	Version        string                 `json:"version"`
	Description    string                 `json:"description"`
	Status         string                 `json:"status"`
	WorkspaceID    string                 `json:"workspaceId"`
	OwnerID        string                 `json:"ownerId"`
	OwnerUsername  string                 `json:"ownerUsername,omitempty"`
	AllowedActions []string               `json:"allowedActions,omitempty"`
	StatusReason   string                 `json:"statusReason,omitempty"`
	LastOperation  string                 `json:"lastOperation,omitempty"`
	LastError      string                 `json:"lastError,omitempty"`
	Revision       int                    `json:"revision"`
	Values         map[string]interface{} `json:"values,omitempty"`
	Replicas       int                    `json:"replicas"`
	CreatedAt      string                 `json:"createdAt"`
	UpdatedAt      string                 `json:"updatedAt"`
}

// InstanceStatusResponse 实例状态响应
type InstanceStatusResponse struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
	Status    string `json:"status"`
	Revision  int    `json:"revision"`
	Chart     string `json:"chart"`
	Version   string `json:"version"`
	UpdatedAt string `json:"updatedAt"`
}

// ReleaseHistoryResponse Release 历史响应
type ReleaseHistoryResponse struct {
	Revision    int    `json:"revision"`
	Updated     string `json:"updated"`
	Status      string `json:"status"`
	Chart       string `json:"chart"`
	AppVersion  string `json:"appVersion"`
	Description string `json:"description"`
}

// InstanceListResponse 实例列表响应
type InstanceListResponse struct {
	Instances []*InstanceResponse `json:"instances"`
	Total     int                 `json:"total"`
}

// InstanceEntryPortResponse Service 端口响应
type InstanceEntryPortResponse struct {
	Name       string `json:"name,omitempty"`
	Protocol   string `json:"protocol"`
	Port       int32  `json:"port"`
	TargetPort string `json:"targetPort,omitempty"`
	NodePort   int32  `json:"nodePort,omitempty"`
}

// InstanceEntryPathResponse Ingress path 响应
type InstanceEntryPathResponse struct {
	Path        string `json:"path"`
	ServiceName string `json:"serviceName,omitempty"`
	ServicePort string `json:"servicePort,omitempty"`
}

// InstanceEntryHostResponse Ingress host 响应
type InstanceEntryHostResponse struct {
	Host  string                      `json:"host"`
	Paths []InstanceEntryPathResponse `json:"paths,omitempty"`
}

// InstanceEntryTLSResponse Ingress TLS 响应
type InstanceEntryTLSResponse struct {
	Hosts      []string `json:"hosts,omitempty"`
	SecretName string   `json:"secretName,omitempty"`
}

// InstanceEntryResponse 实例入口响应
type InstanceEntryResponse struct {
	Kind                string                      `json:"kind"`
	Name                string                      `json:"name"`
	Namespace           string                      `json:"namespace"`
	Type                string                      `json:"type,omitempty"`
	ClusterIP           string                      `json:"clusterIP,omitempty"`
	ExternalIPs         []string                    `json:"externalIPs,omitempty"`
	LoadBalancerIngress []string                    `json:"loadBalancerIngress,omitempty"`
	Ports               []InstanceEntryPortResponse `json:"ports,omitempty"`
	Hosts               []InstanceEntryHostResponse `json:"hosts,omitempty"`
	TLS                 []InstanceEntryTLSResponse  `json:"tls,omitempty"`
}

type InstanceDiagnosticsResponse struct {
	InstanceName string                       `json:"instanceName"`
	Namespace    string                       `json:"namespace"`
	Pods         []InstancePodDiagnostics     `json:"pods"`
	Services     []InstanceServiceDiagnostics `json:"services"`
	Events       []InstanceEventDiagnostics   `json:"events"`
	Logs         []InstancePodLogResponse     `json:"logs"`
	CollectedAt  string                       `json:"collectedAt"`
}

type InstancePodDiagnostics struct {
	Name              string                         `json:"name"`
	Namespace         string                         `json:"namespace"`
	Phase             string                         `json:"phase"`
	NodeName          string                         `json:"nodeName,omitempty"`
	PodIP             string                         `json:"podIp,omitempty"`
	HostIP            string                         `json:"hostIp,omitempty"`
	RestartCount      int32                          `json:"restartCount"`
	Containers        []InstanceContainerDiagnostics `json:"containers"`
	Conditions        []InstanceConditionDiagnostics `json:"conditions"`
	CreationTimestamp string                         `json:"creationTimestamp,omitempty"`
}

type InstanceContainerDiagnostics struct {
	Name         string `json:"name"`
	Image        string `json:"image"`
	Ready        bool   `json:"ready"`
	RestartCount int32  `json:"restartCount"`
	State        string `json:"state"`
	Reason       string `json:"reason,omitempty"`
	Message      string `json:"message,omitempty"`
}

type InstanceConditionDiagnostics struct {
	Type    string `json:"type"`
	Status  string `json:"status"`
	Reason  string `json:"reason,omitempty"`
	Message string `json:"message,omitempty"`
}

type InstanceServiceDiagnostics struct {
	Name      string                      `json:"name"`
	Namespace string                      `json:"namespace"`
	Type      string                      `json:"type"`
	ClusterIP string                      `json:"clusterIP,omitempty"`
	Ports     []InstanceEntryPortResponse `json:"ports,omitempty"`
}

type InstanceEventDiagnostics struct {
	Type           string `json:"type"`
	Reason         string `json:"reason"`
	Message        string `json:"message"`
	InvolvedKind   string `json:"involvedKind"`
	InvolvedName   string `json:"involvedName"`
	Count          int32  `json:"count"`
	FirstTimestamp string `json:"firstTimestamp,omitempty"`
	LastTimestamp  string `json:"lastTimestamp,omitempty"`
}

// ScaleInstanceRequest 扩缩容实例请求
type ScaleInstanceRequest struct {
	Replicas int    `json:"replicas" binding:"required"`
	Workload string `json:"workload"`
}

// ScaleInstanceResponse 扩缩容实例响应
type ScaleInstanceResponse struct {
	Instance *InstanceResponse `json:"instance"`
	Replicas int               `json:"replicas"`
	Message  string            `json:"message"`
}

// InstanceValuesDiffResponse 实例 values 差异响应
type InstanceValuesDiffResponse struct {
	Current  map[string]interface{} `json:"current"`
	Defaults map[string]interface{} `json:"defaults"`
}

type InstancePodLogResponse struct {
	Pod       string `json:"pod"`
	Container string `json:"container"`
	TailLines int64  `json:"tailLines"`
	Log       string `json:"log,omitempty"`
	Error     string `json:"error,omitempty"`
}
