package dto

import (
	"time"

	"github.com/ocdp/cluster-service/internal/domain/entity"
)

// ClusterMetricsResponse 集群监控响应
type ClusterMetricsResponse struct {
	ClusterID            string                      `json:"clusterId"`
	ClusterName          string                      `json:"clusterName"`
	Status               string                      `json:"status"`
	Uptime               string                      `json:"uptime"`
	NodeCount            int                         `json:"nodeCount"`
	PodCount             int                         `json:"podCount"`
	LastCheck            time.Time                   `json:"lastCheck"`
	TotalCPU             string                      `json:"totalCpu"`
	TotalMemory          string                      `json:"totalMemory"`
	TotalGPU             int                         `json:"totalGpu"`
	UsedCPU              string                      `json:"usedCpu"`
	UsedMemory           string                      `json:"usedMemory"`
	UsedGPU              int                         `json:"usedGpu"`
	CPUUsage             float64                     `json:"cpuUsage"`
	MemoryUsage          float64                     `json:"memoryUsage"`
	GPUUsage             float64                     `json:"gpuUsage"`
	CPURequests          string                      `json:"cpuRequests,omitempty"`
	CPULimits            string                      `json:"cpuLimits,omitempty"`
	MemoryRequests       string                      `json:"memoryRequests,omitempty"`
	MemoryLimits         string                      `json:"memoryLimits,omitempty"`
	GPURequests          int64                       `json:"gpuRequests,omitempty"`
	GPULimits            int64                       `json:"gpuLimits,omitempty"`
	GPUMemoryRequestsMB  int64                       `json:"gpuMemoryRequestsMb,omitempty"`
	GPUMemoryLimitsMB    int64                       `json:"gpuMemoryLimitsMb,omitempty"`
	AllocatedGPU         int64                       `json:"allocatedGpu,omitempty"`
	AllocatedGPUMemoryMB int64                       `json:"allocatedGpuMemoryMb,omitempty"`
	ResourceUsageByUser  []UserResourceUsageResponse `json:"resourceUsageByUser,omitempty"`
	MaxNodeCPU           string                      `json:"maxNodeCpu"`
	MaxNodeMemory        string                      `json:"maxNodeMemory"`
	MaxNodeGPU           int                         `json:"maxNodeGpu"`
	MaxNodeCPUUsage      float64                     `json:"maxNodeCpuUsage"`
	MaxNodeMemUsage      float64                     `json:"maxNodeMemUsage"`
	MaxNodeGPUUsage      float64                     `json:"maxNodeGpuUsage"`
	Nodes                []NodeMetricsResponse       `json:"nodes,omitempty"`
}

type UserResourceUsageResponse struct {
	UserID              string `json:"userId"`
	Username            string `json:"username"`
	WorkspaceID         string `json:"workspaceId"`
	InstanceCount       int    `json:"instanceCount"`
	PodCount            int    `json:"podCount"`
	CPURequests         string `json:"cpuRequests"`
	CPULimits           string `json:"cpuLimits"`
	MemoryRequests      string `json:"memoryRequests"`
	MemoryLimits        string `json:"memoryLimits"`
	GPURequests         int64  `json:"gpuRequests"`
	GPULimits           int64  `json:"gpuLimits"`
	GPUMemoryRequestsMB int64  `json:"gpuMemoryRequestsMb"`
	GPUMemoryLimitsMB   int64  `json:"gpuMemoryLimitsMb"`
}

// NodeMetricsResponse 节点监控响应
type NodeMetricsResponse struct {
	NodeName          string  `json:"nodeName"`
	Status            string  `json:"status"`
	Role              string  `json:"role"`
	Age               string  `json:"age"`
	PodCount          int     `json:"podCount"`
	CPUCapacity       string  `json:"cpuCapacity"`
	CPUAllocatable    string  `json:"cpuAllocatable"`
	CPUUsage          string  `json:"cpuUsage"`
	CPUPercent        float64 `json:"cpuPercent"`
	MemoryCapacity    string  `json:"memoryCapacity"`
	MemoryAllocatable string  `json:"memoryAllocatable"`
	MemoryUsage       string  `json:"memoryUsage"`
	MemoryPercent     float64 `json:"memoryPercent"`
	GPUCapacity       int     `json:"gpuCapacity"`
	GPUUsage          int     `json:"gpuUsage"`
	GPUPercent        float64 `json:"gpuPercent"`
	GPUType           string  `json:"gpuType,omitempty"`
	OSImage           string  `json:"osImage,omitempty"`
	KernelVersion     string  `json:"kernelVersion,omitempty"`
	ContainerRuntime  string  `json:"containerRuntime,omitempty"`
	KubeletVersion    string  `json:"kubeletVersion,omitempty"`
}

// MonitoringSummaryResponse 监控汇总响应
type MonitoringSummaryResponse struct {
	TotalClusters   int       `json:"totalClusters"`
	HealthyClusters int       `json:"healthyClusters"`
	WarningClusters int       `json:"warningClusters"`
	ErrorClusters   int       `json:"errorClusters"`
	TotalNodes      int       `json:"totalNodes"`
	TotalPods       int       `json:"totalPods"`
	LastUpdate      time.Time `json:"lastUpdate"`
}

// ToClusterMetricsResponse 转换为响应
func ToClusterMetricsResponse(m *entity.ClusterMetrics) *ClusterMetricsResponse {
	resp := &ClusterMetricsResponse{
		ClusterID:            m.ClusterID,
		ClusterName:          m.ClusterName,
		Status:               m.Status,
		Uptime:               m.Uptime,
		NodeCount:            m.NodeCount,
		PodCount:             m.PodCount,
		LastCheck:            m.LastCheck,
		TotalCPU:             m.TotalCPU,
		TotalMemory:          m.TotalMemory,
		TotalGPU:             m.TotalGPU,
		UsedCPU:              m.UsedCPU,
		UsedMemory:           m.UsedMemory,
		UsedGPU:              m.UsedGPU,
		CPUUsage:             m.CPUUsage,
		MemoryUsage:          m.MemoryUsage,
		GPUUsage:             m.GPUUsage,
		CPURequests:          m.CPURequests,
		CPULimits:            m.CPULimits,
		MemoryRequests:       m.MemoryRequests,
		MemoryLimits:         m.MemoryLimits,
		GPURequests:          m.GPURequests,
		GPULimits:            m.GPULimits,
		GPUMemoryRequestsMB:  m.GPUMemoryRequestsMB,
		GPUMemoryLimitsMB:    m.GPUMemoryLimitsMB,
		AllocatedGPU:         m.AllocatedGPU,
		AllocatedGPUMemoryMB: m.AllocatedGPUMemoryMB,
		MaxNodeCPU:           m.MaxNodeCPU,
		MaxNodeMemory:        m.MaxNodeMemory,
		MaxNodeGPU:           m.MaxNodeGPU,
		MaxNodeCPUUsage:      m.MaxNodeCPUUsage,
		MaxNodeMemUsage:      m.MaxNodeMemUsage,
		MaxNodeGPUUsage:      m.MaxNodeGPUUsage,
	}

	if len(m.ResourceUsageByUser) > 0 {
		resp.ResourceUsageByUser = make([]UserResourceUsageResponse, len(m.ResourceUsageByUser))
		for i, usage := range m.ResourceUsageByUser {
			resp.ResourceUsageByUser[i] = UserResourceUsageResponse{
				UserID:              usage.UserID,
				Username:            usage.Username,
				WorkspaceID:         usage.WorkspaceID,
				InstanceCount:       usage.InstanceCount,
				PodCount:            usage.PodCount,
				CPURequests:         usage.CPURequests,
				CPULimits:           usage.CPULimits,
				MemoryRequests:      usage.MemoryRequests,
				MemoryLimits:        usage.MemoryLimits,
				GPURequests:         usage.GPURequests,
				GPULimits:           usage.GPULimits,
				GPUMemoryRequestsMB: usage.GPUMemoryRequestsMB,
				GPUMemoryLimitsMB:   usage.GPUMemoryLimitsMB,
			}
		}
	}

	if len(m.Nodes) > 0 {
		resp.Nodes = make([]NodeMetricsResponse, len(m.Nodes))
		for i, node := range m.Nodes {
			resp.Nodes[i] = NodeMetricsResponse{
				NodeName:          node.NodeName,
				Status:            node.Status,
				Role:              node.Role,
				Age:               node.Age,
				PodCount:          node.PodCount,
				CPUCapacity:       node.CPUCapacity,
				CPUAllocatable:    node.CPUAllocatable,
				CPUUsage:          node.CPUUsage,
				CPUPercent:        node.CPUPercent,
				MemoryCapacity:    node.MemoryCapacity,
				MemoryAllocatable: node.MemoryAllocatable,
				MemoryUsage:       node.MemoryUsage,
				MemoryPercent:     node.MemoryPercent,
				GPUCapacity:       node.GPUCapacity,
				GPUUsage:          node.GPUUsage,
				GPUPercent:        node.GPUPercent,
				GPUType:           node.GPUType,
				OSImage:           node.OSImage,
				KernelVersion:     node.KernelVersion,
				ContainerRuntime:  node.ContainerRuntime,
				KubeletVersion:    node.KubeletVersion,
			}
		}
	}

	return resp
}

// ToMonitoringSummaryResponse 转换为汇总响应
func ToMonitoringSummaryResponse(s *entity.MonitoringSummary) *MonitoringSummaryResponse {
	return &MonitoringSummaryResponse{
		TotalClusters:   s.TotalClusters,
		HealthyClusters: s.HealthyClusters,
		WarningClusters: s.WarningClusters,
		ErrorClusters:   s.ErrorClusters,
		TotalNodes:      s.TotalNodes,
		TotalPods:       s.TotalPods,
		LastUpdate:      s.LastUpdate,
	}
}
