package processor

import (
	"encoding/json"
	"fmt"

	"nms_nbi/src/framework/logger"
	"nms_nbi/src/framework/vo/result"
	neDataService "nms_nbi/src/modules/network_data/service"
	neService "nms_nbi/src/modules/network_element/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.NewNeInfoImpl.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
	if neInfo.NeId != querys.NeID || neInfo.IP == "" {
		return nil, fmt.Errorf("no matching network element information found")
	}

	dataMap := neDataService.NewPerfKPIImpl.SelectUPFTotalFlow(neInfo.NeType, neInfo.RmUID, querys.Day)

	resultByte, err := json.Marshal(result.Ok(map[string]any{
		"requestId": requestID,
		"data":      dataMap,
	}))
	return resultByte, err
}
