package processor

import (
	"encoding/json"
	"fmt"

	"be.ems/src/framework/logger"
	"be.ems/src/framework/resp"
	neService "be.ems/src/modules/ne/service"
	neDataService "be.ems/src/modules/ne_data/service"
)

// GetUPFTotalFlow 获取UPF-总流量数 N3上行 N6下行 单位比特(bit)
func GetUPFTotalFlow(requestID string, data any) ([]byte, error) {
	msgByte, _ := json.Marshal(data)
	var querys struct {
		NeType string `json:"neType" form:"neType" binding:"required"`
		NeID   string `json:"neId" form:"neId" binding:"required"`
		Day    int    `json:"day" binding:"required"`
	}
	err := json.Unmarshal(msgByte, &querys)
	if err != nil {
		logger.Warnf("ws processor GetUPFTotalFlow err: %s", err.Error())
		return nil, fmt.Errorf("query data structure error")
	}

	if querys.NeType == "" || querys.NeID == "" || querys.Day < 0 {
		return nil, fmt.Errorf("query neType any neId empty or day less 0 ")
	}

	// 查询网元获取IP
	neInfo := neService.NewNeInfo.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
	if neInfo.NeId != querys.NeID || neInfo.IP == "" {
		return nil, fmt.Errorf("no matching network element information found")
	}

	up, down := neDataService.NewKpiReport.UPFTodayFlowFind(neInfo.RmUID, querys.Day)

	resultByte, err := json.Marshal(resp.Ok(map[string]any{
		"requestId": requestID,
		"data":      map[string]int64{"up": up, "down": down},
	}))
	return resultByte, err
}
