package controller

import (
	"encoding/json"
	"nms_nbi/src/framework/i18n"
	"nms_nbi/src/framework/utils/ctx"
	"nms_nbi/src/framework/vo/result"
	"nms_nbi/src/modules/api_rest_cxy/service"
	"nms_nbi/src/modules/api_rest_cxy/utils/kafka"
	"nms_nbi/src/modules/api_rest_cxy/utils/oss"
	neDataModel "nms_nbi/src/modules/network_data/model"
	neService "nms_nbi/src/modules/network_element/service"

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

// 实例化控制层 TestController 结构体
var NewTest = &TestController{
	neInfoService:      neService.NewNeInfoImpl,
	alarmService:       service.NewAlarmImpl,
	configService:      service.NewConfigImpl,
	performanceService: service.NewPerformanceImpl,
}

// 测试接口
//
// PATH /testManagement
type TestController struct {
	// 网元信息服务
	neInfoService neService.INeInfo
	// 告警数据服务
	alarmService service.IAlarm
	// 配置数据处理服务
	configService service.IConfig
	// 性能数据处理服务
	performanceService service.IPerformance
}

// kafka发送消息
//
// POST /kafka/send
func (s *TestController) KafkaSend(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		RequestId string `json:"requestId" binding:"required"`
		Topic     string `json:"topic" binding:"required"`
		Partition int    `json:"partition"`
		Msg       any    `json:"msg"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	bytes, err := json.Marshal(body.Msg)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, err.Error()))
		return
	}

	// 发送消息
	partition, offset, err := kafka.KInitConm.MessageSyncSend(body.Topic, int32(body.Partition), string(bytes))
	if err != nil {
		c.JSON(200, map[string]any{
			"requestId": body.RequestId,
			"msg":       body.Msg,
			"error":     err.Error(),
		})
		return
	}
	c.JSON(200, map[string]any{
		"requestId": body.RequestId,
		"partition": partition,
		"offset":    offset,
	})
}

// kafka读取并消费消息
//
// POST /kafka/readMark
func (s *TestController) KafkaReadMark(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		RequestId string `json:"requestId" binding:"required"`
		Topic     string `json:"topic" binding:"required"`
		Partition int    `json:"partition"`
		Group     string `json:"group" binding:"required"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 消费消息
	msg, err := kafka.KInitConm.MessageSyncReadMark(body.Topic, int32(body.Partition), body.Group)
	if err != nil {
		c.JSON(200, map[string]any{
			"requestId": body.RequestId,
			"error":     err.Error(),
		})
		return
	}
	c.JSON(200, map[string]any{
		"requestId": body.RequestId,
		"msg":       msg,
	})
}

// 告警列表
//
// POST /alarm/list
func (s *TestController) AlarmList(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		RequestId  string `json:"requestId" binding:"required"`
		Ruid       string `json:"ruid" binding:"required"`
		AlarmLevel string `json:"alarmLevel" form:"alarmLevel"` // 告警类型 1: Critical, 2: Major, 3: Minor, 4: Warning, 5: Event(Only VNF)
		StartTime  string `json:"startTime" form:"startTime"`
		EndTime    string `json:"endTime" form:"endTime"`
		SortField  string `json:"sortField" form:"sortField" binding:"omitempty,oneof=timestamp"` // 排序字段，填写结果字段
		SortOrder  string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"`  // 排序升降序，asc desc
		PageNum    int64  `json:"pageNum" form:"pageNum" binding:"required"`
		PageSize   int64  `json:"pageSize" form:"pageSize" binding:"required"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	neInfo := s.neInfoService.SelectNeInfoByRmuid(body.Ruid)
	if neInfo.RmUID != body.Ruid {
		c.JSON(500, map[string]any{
			"requestId": body.RequestId,
			"errorInfo": "NE not exist",
		})
		return
	}

	rows, total := s.alarmService.SelectPage(neDataModel.AlarmQuery{
		PageNum:      body.PageNum,
		PageSize:     body.PageSize,
		SortField:    body.SortField,
		SortOrder:    body.SortOrder,
		StartTime:    body.StartTime,
		EndTime:      body.EndTime,
		RmUID:        neInfo.RmUID,
		NeType:       neInfo.NeType,
		OrigSeverity: body.AlarmLevel,
	})
	c.JSON(200, map[string]any{
		"requestId": body.RequestId,
		"rows":      rows,
		"total":     total,
	})
}

// OSS文件上传
//
// POST /oss/upload
func (s *TestController) OssUpload(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		RequestId   string `json:"requestId" binding:"required"`
		FilePath    string `json:"filePath" binding:"required"`
		NewFilePath string `json:"newFilePath" binding:"required"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	// 消费消息
	msg, err := oss.FileUploadFileByFilePath(body.FilePath, body.NewFilePath)
	if err != nil {
		c.JSON(200, map[string]any{
			"requestId": body.RequestId,
			"error":     err.Error(),
		})
		return
	}
	c.JSON(200, map[string]any{
		"requestId": body.RequestId,
		"msg":       msg,
	})
}

// OSS文件上传测试上报
//
// POST /oss/testUp
func (s *TestController) OssTestUp(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		RequestId string `json:"requestId" binding:"required"`
		OperType  string `json:"operType" binding:"required"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	if body.OperType == "config" {
		err := s.configService.ConfigUploadOSS("OMC")
		c.JSON(200, map[string]any{
			"requestId": body.RequestId,
			"operType":  body.OperType,
			"err":       err,
		})
		return
	}
	if body.OperType == "performance" {
		err := s.performanceService.PerformanceUploadOSS("AMF")
		c.JSON(200, map[string]any{
			"requestId": body.RequestId,
			"operType":  body.OperType,
			"err":       err,
		})
		return
	}

	c.JSON(200, map[string]any{
		"requestId": body.RequestId,
		"operType":  body.OperType,
	})
}
