package com.qqchen.deploy.backend.deploy.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务器监控指标响应
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "服务器监控指标数据")
public class ServerMonitorMetricsDTO {

    @Schema(description = "服务器信息")
    private ServerInfo server;

    @Schema(description = "时间范围信息")
    private TimeRangeInfo timeRange;

    @Schema(description = "指标数据")
    private MetricsData metrics;

    @Schema(description = "统计信息")
    private StatisticsInfo statistics;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "服务器信息")
    public static class ServerInfo {
        @Schema(description = "服务器ID")
        private Long serverId;
        
        @Schema(description = "服务器名称")
        private String serverName;
        
        @Schema(description = "主机IP")
        private String hostIp;
        
        @Schema(description = "服务器状态")
        private String status;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "时间范围信息")
    public static class TimeRangeInfo {
        @Schema(description = "开始时间")
        private LocalDateTime startTime;
        
        @Schema(description = "结束时间")
        private LocalDateTime endTime;
        
        @Schema(description = "聚合间隔")
        private String interval;
        
        @Schema(description = "实际数据点数量")
        private Integer dataPoints;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "指标数据")
    public static class MetricsData {
        @Schema(description = "CPU指标数据")
        private List<CpuMetric> cpu;
        
        @Schema(description = "内存指标数据")
        private List<MemoryMetric> memory;
        
        @Schema(description = "网络指标数据")
        private List<NetworkMetric> network;
        
        @Schema(description = "磁盘指标数据")
        private DiskMetric disk;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "CPU指标")
    public static class CpuMetric {
        @Schema(description = "时间点")
        private LocalDateTime time;
        
        @Schema(description = "CPU使用率(%)")
        private BigDecimal value;
        
        @Schema(description = "采集状态")
        private String status;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "内存指标")
    public static class MemoryMetric {
        @Schema(description = "时间点")
        private LocalDateTime time;
        
        @Schema(description = "内存使用率(%)")
        private BigDecimal usagePercent;
        
        @Schema(description = "已用内存(GB)")
        private Integer usedGB;
        
        @Schema(description = "采集状态")
        private String status;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "网络指标")
    public static class NetworkMetric {
        @Schema(description = "时间点")
        private LocalDateTime time;
        
        @Schema(description = "接收字节数")
        private Long rxBytes;
        
        @Schema(description = "发送字节数")
        private Long txBytes;
        
        @Schema(description = "接收速率(MB/s)")
        private BigDecimal rxMBps;
        
        @Schema(description = "发送速率(MB/s)")
        private BigDecimal txMBps;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "磁盘指标")
    public static class DiskMetric {
        @Schema(description = "最新采集时间")
        private LocalDateTime latestTime;
        
        @Schema(description = "分区信息列表")
        private List<DiskPartition> partitions;
        
        @Schema(description = "时间范围内最大使用率(%)")
        private BigDecimal maxUsagePercent;
        
        @Schema(description = "最大使用率的分区")
        private String maxUsagePartition;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "磁盘分区")
    public static class DiskPartition {
        @Schema(description = "挂载点")
        private String mountPoint;
        
        @Schema(description = "文件系统")
        private String fileSystem;
        
        @Schema(description = "总容量(GB)")
        private Long totalSizeGB;
        
        @Schema(description = "已用容量(GB)")
        private Long usedSizeGB;
        
        @Schema(description = "使用率(%)")
        private BigDecimal usagePercent;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "统计信息")
    public static class StatisticsInfo {
        @Schema(description = "CPU统计")
        private CpuStats cpu;
        
        @Schema(description = "内存统计")
        private MemoryStats memory;
        
        @Schema(description = "网络统计")
        private NetworkStats network;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "CPU统计")
    public static class CpuStats {
        @Schema(description = "平均值(%)")
        private BigDecimal avg;
        
        @Schema(description = "最大值(%)")
        private BigDecimal max;
        
        @Schema(description = "最小值(%)")
        private BigDecimal min;
        
        @Schema(description = "峰值时间")
        private LocalDateTime maxTime;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "内存统计")
    public static class MemoryStats {
        @Schema(description = "平均使用率(%)")
        private BigDecimal avgPercent;
        
        @Schema(description = "最大使用率(%)")
        private BigDecimal maxPercent;
        
        @Schema(description = "最小使用率(%)")
        private BigDecimal minPercent;
        
        @Schema(description = "峰值时间")
        private LocalDateTime maxTime;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "网络统计")
    public static class NetworkStats {
        @Schema(description = "总接收字节数")
        private Long totalRxBytes;
        
        @Schema(description = "总发送字节数")
        private Long totalTxBytes;
        
        @Schema(description = "平均接收速率(MB/s)")
        private BigDecimal avgRxMBps;
        
        @Schema(description = "平均发送速率(MB/s)")
        private BigDecimal avgTxMBps;
        
        @Schema(description = "峰值接收速率(MB/s)")
        private BigDecimal peakRxMBps;
        
        @Schema(description = "峰值发送速率(MB/s)")
        private BigDecimal peakTxMBps;
    }
}
