package mf_callback_ticket

import (
	"strconv"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/middleware"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/vo/result"
	"github.com/gin-gonic/gin"
)

// @Description Register Routes for mf calling
func Register(r *gin.RouterGroup) {

	mfCallingGroup := r.Group("/ticket")
	{
		var m *CallbackTicket
		mfCallingGroup.GET("/list",
			middleware.PreAuthorize(nil),
			m.List,
		)
		mfCallingGroup.PUT("/:ticketId",
			middleware.PreAuthorize(nil),
			m.Update,
		)
		mfCallingGroup.DELETE("/:ticketId",
			middleware.PreAuthorize(nil),
			m.Delete,
		)
		mfCallingGroup.GET("/:ticketId",
			middleware.PreAuthorize(nil),
			m.GetById,
		)
	}
}

func (m *CallbackTicket) List(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	neId := c.Query("neId")
	if neId == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	var query CallbackTicketQuery
	if err := c.ShouldBindQuery(&query); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	service := NewCallbackTicketService()
	data, total, err := service.SelectCallbackTicket(query)
	if err != nil {
		c.JSON(500, result.ErrMsg(err.Error()))
		return
	}

	c.JSON(200, result.Ok(gin.H{
		"total": total,
		"data":  data,
	}))
}

// Update 更新回调工单
func (m *CallbackTicket) Update(c *gin.Context) {
	language := ctx.AcceptLanguage(c)

	// 获取路径参数
	ticketId := c.Param("ticketId")

	// 绑定请求体
	var ticket CallbackTicket
	if err := c.ShouldBindJSON(&ticket); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查路径ID和请求体ID是否一致
	if ticketId != "" {
		id, err := strconv.ParseInt(ticketId, 10, 64)
		if err != nil {
			c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
			return
		}
		ticket.TicketId = id
	}

	service := NewCallbackTicketService()
	if err := service.UpdateCallbackTicket(ticket); err != nil {
		c.JSON(500, result.ErrMsg(err.Error()))
		return
	}

	c.JSON(200, result.Ok(nil))
}

// Delete 删除回调工单
func (m *CallbackTicket) Delete(c *gin.Context) {
	language := ctx.AcceptLanguage(c)

	// 获取路径参数
	ticketId := c.Param("ticketId")
	if ticketId == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	id, err := strconv.ParseInt(ticketId, 10, 64)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	service := NewCallbackTicketService()
	if err := service.DeleteCallbackTicket(id); err != nil {
		c.JSON(500, result.ErrMsg(err.Error()))
		return
	}

	c.JSON(200, result.Ok(nil))
}

// GetById 根据ID获取回调工单
func (m *CallbackTicket) GetById(c *gin.Context) {
	language := ctx.AcceptLanguage(c)

	// 获取路径参数
	ticketId := c.Param("ticketId")
	if ticketId == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	id, err := strconv.ParseInt(ticketId, 10, 64)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	service := NewCallbackTicketService()
	ticket, err := service.SelectCallbackTicketById(id)
	if err != nil {
		c.JSON(500, result.ErrMsg(err.Error()))
		return
	}

	if ticket == nil {
		c.JSON(404, result.CodeMsg(404, i18n.TKey(language, "app.common.err404")))
		return
	}

	c.JSON(200, result.Ok(gin.H{
		"ticket": ticket,
	}))
}
