package controller

import (
	"fmt"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	neService "be.ems/src/modules/ne/service"
	neDataService "be.ems/src/modules/ne_data/service"

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

// 实例化控制层 UPFController 结构体
var NewUPF = &UPFController{
	neInfoService:    neService.NewNeInfo,
	kpiReportService: neDataService.NewKpiReport,
}

// 网元UPF
//
// PATH /upf
type UPFController struct {
	neInfoService    *neService.NeInfo        // 网元信息服务
	kpiReportService *neDataService.KpiReport // 统计信息服务
}

// 总流量数 N3上行 N6下行
// 单位 比特(bit)
//
// GET /flow-total
//
//	@Tags			ne_data/upf
//	@Accept			json
//	@Produce		json
//	@Param			neId	query		string	true	"NE ID"									default(001)
//	@Param			day		query		number	true	"Statistical time a few days before"	Enums(0, 7, 30)
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Total number of flows N3 upstream N6 downstream
//	@Description	Total number of flows N3 upstream N6 downstream
//	@Router			/neData/upf/flow-total [get]
func (s UPFController) FlowTotal(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var querys struct {
		NeID string `form:"neId" binding:"required"`
		Day  int    `form:"day"`
	}
	if err := c.ShouldBindQuery(&querys); querys.Day < 0 || err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

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

	up, down := s.kpiReportService.UPFTodayFlowFind(neInfo.RmUID, querys.Day)
	c.JSON(200, resp.OkData(map[string]int64{"up": up, "down": down}))
}
