package system

import (
	"sundynix-go/global"
	"sundynix-go/model/commom/response"
	"sundynix-go/model/system"
	systemReq "sundynix-go/model/system/request"
	"sundynix-go/utils/auth"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MenuApi struct {
}

// SaveMenu
// @Tags      菜单管理
// @Summary   新增菜单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      system.Menu  false  "menu"
// @Success   200   {object}  response.Response{msg=string}  "新建菜单/按钮"
// @Router    /menu/save [post]
func (m *MenuApi) SaveMenu(c *gin.Context) {
	var menu system.Menu
	err := c.ShouldBindJSON(&menu)
	if err != nil {
		response.FailWithMsg("参数错误:"+err.Error(), c)
		return
	}
	if err = menuService.SaveMenu(menu); err != nil {
		global.Logger.Error("保存菜单失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	} else {
		response.OkWithMsg("保存菜单成功!", c)
	}
}

// UpdateMenu
// @Tags      菜单管理
// @Summary   更新菜单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      system.Menu  false  "menu"
// @Success   200   {object}  response.Response{msg=string}  "更新菜单"
// @Router    /menu/update [post]
func (m *MenuApi) UpdateMenu(c *gin.Context) {
	var menu system.Menu
	err := c.ShouldBindJSON(&menu)
	if err != nil {
		response.FailWithMsg("参数错误:"+err.Error(), c)
		return
	}
	if err = menuService.UpdateMenu(&menu); err != nil {
		global.Logger.Error("更新菜单失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	} else {
		response.OkWithMsg("更新菜单成功!", c)
	}
}

// DeleteMenu
// @Tags      菜单管理
// @Summary   删除menu
// @Description  删除menu
// @Security  ApiKeyAuth
// @Produce   application/json
// @Param     id  query  string  true  "id"
// @Success   200   {object}  response.Response{msg=string}  "详情"
// @Router    /menu/delete [get]
func (m *MenuApi) DeleteMenu(c *gin.Context) {
	id := c.Query("id")
	err := menuService.DeleteMenu(id)
	if err != nil {
		global.Logger.Error("删除菜单失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("删除菜单成功!", c)
}

// Detail
// @Tags      菜单管理
// @Summary   获取menu详情
// @Description  id获取详情
// @Security  ApiKeyAuth
// @Produce   application/json
// @Param     id  query  string  true  "id"
// @Success   200   {object}  response.Response{data=system.Menu,msg=string}  "详情"
// @Router    /menu/detail [get]
func (m *MenuApi) Detail(c *gin.Context) {
	id := c.Query("id")
	menu, err := menuService.GetMenuById(id)
	if err != nil {
		global.Logger.Error("获取菜单详情失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(menu, c)
}

// GetAllMenuTree
// @Tags 菜单管理
// @Summary 获取所有菜单树
// @Security ApiKeyAuth
// @Accept  json
// @Produce  json
// @Param  data  body  systemReq.GetMenuTree true "菜单信息"
// @Success   200   {object}  response.Response{data=[]system.Menu,msg=string}  "获取所有菜单树"
// @Router /menu/getAllMenuTree [post]
func (m *MenuApi) GetAllMenuTree(c *gin.Context) {
	var param systemReq.GetMenuTree
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	menus, err := menuService.GetAllMenuTree(param.Category, param.ParentId)
	if err != nil {
		global.Logger.Error("获取菜单树结构失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(menus, c)
}

// GetUserMenuTree
// @Tags 菜单管理
// @Summary 用户菜单数据
// @Security ApiKeyAuth
// @Produce  json
// @Success   200   {object}  response.Response{data=[]system.Menu,msg=string}  "用户菜单数据"
// @Router /menu/getUserMenuTree [get]
func (m *MenuApi) GetUserMenuTree(c *gin.Context) {
	userId := auth.GetUserId(c)
	routes, err := menuService.GetUserRoutes(userId)
	if err != nil {
		global.Logger.Error("获取用户菜单失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(routes, c)
}

// Route
// @Tags 菜单管理
// @Summary 用户路由
// @Security ApiKeyAuth
// @Produce  json
// @Success   200   {object}  response.Response{data=[]system.Menu,msg=string}  "用户route"
// @Router /menu/route [get]
func (m *MenuApi) Route(c *gin.Context) {
	userId := auth.GetUserId(c)
	routes, err := menuService.GetUserRoutes(userId)
	if err != nil {
		global.Logger.Error("获取用户菜单失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(routes, c)
}
