package controller

import (
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/vo/result"
	"be.ems/src/modules/network_data/model"
	neDataService "be.ems/src/modules/network_data/service"
	neService "be.ems/src/modules/network_element/service"
	"github.com/gin-gonic/gin"
)

// 实例化控制层 PerfKPIController 结构体
var NewPerfKPI = &PerfKPIController{
	neInfoService:  neService.NewNeInfo,
	perfKPIService: neDataService.NewPerfKPI,
}

// 性能统计
//
// PATH /kpi
type PerfKPIController struct {
	neInfoService  *neService.NeInfo      // 网元信息服务
	perfKPIService *neDataService.PerfKPI // 统计信息服务
}

// 获取统计数据
//
// GET /data
//
//	@Tags			network_data/kpi
//	@Accept			json
//	@Produce		json
//	@Param			neType		query		string	true	"NE Type"								Enums(IMS,AMF,AUSF,UDM,SMF,PCF,NSSF,NRF,UPF,MME,CBC,OMC,SGWC) default(AMF)
//	@Param			neId		query		string	true	"NE ID"									default(001)
//	@Param			startTime	query		number	true	"Start time (timestamped milliseconds)"	default(1729162507596)
//	@Param			endTime		query		number	true	"End time (timestamped milliseconds)"	default(1729164187611)
//	@Param			interval	query		number	true	"interval"								Enums(5,10,15,30,60,300,600,900,1800,3600) default(60)
//	@Success		200			{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Access to statistical data
//	@Description	Access to statistical data
//	@Router			/neData/kpi/data [get]
func (s *PerfKPIController) GoldKPI(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var querys model.GoldKPIQuery
	if err := c.ShouldBindQuery(&querys); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	if querys.Interval < 5 || querys.Interval > 3600 {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 查询网元获取IP
	neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
	if neInfo.NeId != querys.NeID || neInfo.IP == "" {
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
		return
	}
	querys.RmUID = neInfo.RmUID

	// 查询数据
	kpiData := s.perfKPIService.SelectGoldKPI(querys)
	c.JSON(200, result.OkData(kpiData))
}

// 获取统计标题
//
// GET /title
//
//	@Tags			network_data/kpi
//	@Accept			json
//	@Produce		json
//	@Param			neType	query		string	true	"NE Type"	Enums(IMS,AMF,AUSF,UDM,SMF,PCF,NSSF,NRF,UPF,MME,CBC,OMC,SGWC) default(AMF)
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Get Statistical Headings
//	@Description	Get Statistical Headings
//	@Router			/neData/kpi/title [get]
func (s *PerfKPIController) Title(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	neType := c.Query("neType")
	if neType == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	kpiTitles := s.perfKPIService.SelectGoldKPITitle(neType)

	c.JSON(200, result.OkData(kpiTitles))
}
