package com.qqchen.deploy.backend.deploy.api;

import com.qqchen.deploy.backend.deploy.dto.ServerMonitorMetricsDTO;
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorMetricsQuery;
import com.qqchen.deploy.backend.deploy.service.IServerMonitorService;
import com.qqchen.deploy.backend.framework.api.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 服务器监控数据 Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/server/monitor")
@Tag(name = "服务器监控数据", description = "服务器监控数据查询接口")
public class ServerMonitorApiController {

    @Resource
    private IServerMonitorService serverMonitorService;

    @Operation(
        summary = "查询服务器监控指标数据",
        description = "支持快捷时间范围（最近1小时/6小时/24小时/7天/30天）和自定义时间范围，" +
                     "支持按指标类型查询（CPU/MEMORY/DISK/NETWORK），自动根据时间范围选择合适的聚合粒度"
    )
    @GetMapping("/{serverId}/metrics")
    public Response<ServerMonitorMetricsDTO> getServerMetrics(
        @Parameter(description = "服务器ID", required = true) 
        @PathVariable Long serverId,
        
        @Parameter(description = "查询参数（快捷时间范围、自定义时间、指标类型等）") 
        @ModelAttribute ServerMonitorMetricsQuery query
    ) {
        // 设置服务器ID（从路径参数）
        query.setServerId(serverId);
        
        log.info("查询服务器监控指标: serverId={}, timeRange={}, metrics={}", 
            serverId, query.getTimeRange(), query.getMetrics());
        
        ServerMonitorMetricsDTO response = serverMonitorService.getServerMetrics(query);
        return Response.success(response);
    }
}
