package controller

import (
	"fmt"

	"be.ems/src/framework/resp"
	"be.ems/src/modules/monitor/service"

	"github.com/gin-gonic/gin"
)

// 实例化控制层 MonitorInfoController 结构体
var NewMonitor = &MonitorController{
	monitorService: service.NewMonitor,
}

// 服务器资源监控信息
//
// PATH /monitor
type MonitorController struct {
	monitorService *service.Monitor // 服务器系统相关信息服务
}

// 资源监控信息加载
//
// GET /load
//
//	@Tags			monitor
//	@Accept			json
//	@Produce		json
//	@Param			type		query		string	true	"Data Type"							Enums(all,load,cpu,memory,io,network)	default(all)
//	@Param			beginTime	query		number	true	"BeginTime, timestamp milliseconds"	default(1738771200000)
//	@Param			endTime		query		number	true	"EndTime, timestamp milliseconds"	default(1738810051253)
//	@Param			neType		query		string	false	"NE Type, Currently none Default #"	default(#)
//	@Param			neId		query		string	false	"NE ID, Currently none Default #"	default(#)
//	@Param			name		query		string	false	"Name, Data Type valid for networ and io"
//	@Success		200			{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Load Resource Utilization Information
//	@Description	Load Resource Utilization Information
//	@Router			/monitor/load [get]
func (s *MonitorController) Load(c *gin.Context) {
	var querys struct {
		Type      string `form:"type" binding:"required,oneof=all load cpu memory io network"` // 数据类型all/load/cpu/memory/io/network
		BeginTime int64  `form:"beginTime" binding:"required"`                                 // 开始时间
		EndTime   int64  `form:"endTime" binding:"required"`                                   // 结束时间
		NeType    string `form:"neType"`                                                       // 网元类型
		NeID      string `form:"neId"`                                                         // 网元ID
		Name      string `form:"name"`                                                         // 名称，networ和io时有效
	}
	if err := c.ShouldBindQuery(&querys); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 查询数据
	data := s.monitorService.FindInfo(map[string]any{
		"type":      querys.Type,
		"beginTime": querys.BeginTime,
		"endTime":   querys.EndTime,
		"neType":    querys.NeType,
		"neId":      querys.NeID,
		"name":      querys.Name,
	})
	c.JSON(200, resp.OkData(data))
}
