package controller

import (
	"fmt"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	neFetchlink "be.ems/src/modules/ne/fetch_link"
	neService "be.ems/src/modules/ne/service"

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

// 实例化控制层 N3IWFController 结构体
var NewN3IWF = &N3IWFController{
	neInfoService: neService.NewNeInfo,
}

// 网元N3IWF
//
// PATH /n3iwf
type N3IWFController struct {
	neInfoService *neService.NeInfo // 网元信息服务
}

// 在线订阅用户列表信息
//
// GET /sub/list
//
//	@Tags			ne_data/n3iwf
//	@Accept			json
//	@Produce		json
//	@Param			neId	query		string	true	"NE ID"	default(001)
//	@Param			imsi	query		string	false	"imsi"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Online session user list information
//	@Description	Online session user list information
//	@Router			/neData/n3iwf/sub/list [get]
func (s N3IWFController) SubUserList(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var query struct {
		NeId string `form:"neId" binding:"required"`
		IMSI string `form:"imsi"`
	}
	if err := c.ShouldBindQuery(&query); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 查询网元信息
	neInfo := s.neInfoService.FindByNeTypeAndNeID("N3IWF", query.NeId)
	if neInfo.NeId != query.NeId || neInfo.IP == "" {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
		return
	}

	// 网元直连
	data, err := neFetchlink.N3IWFSubInfoList(neInfo, map[string]string{
		"imsi": query.IMSI,
	})
	if err != nil {
		c.JSON(200, resp.ErrMsg(err.Error()))
		return
	}

	c.JSON(200, resp.OkData(data))
}
