// Code generated by hertz generator.

package admin

import (
	"context"
	admin2 "formulago/biz/domain/admin"
	logic "formulago/biz/logic/admin"
	"formulago/data"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/jinzhu/copier"
	"strconv"

	"formulago/api/model/admin"
	base "formulago/api/model/base"
	"github.com/cloudwego/hertz/pkg/app"
)

// CreateMenu .
// @router /api/admin/menu/create [POST]
func CreateMenu(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateMenuReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var menuReq admin2.MenuInfo
	err = copier.Copy(&menuReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	err = logic.NewMenu(data.Default()).Create(ctx, &menuReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateMenu .
// @router /api/admin/menu/update [POST]
func UpdateMenu(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateMenuReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var menuReq admin2.MenuInfo
	err = copier.Copy(&menuReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	err = logic.NewMenu(data.Default()).Update(ctx, &menuReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DeleteMenu .
// @router /api/admin/menu [DELETE]
func DeleteMenu(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewMenu(data.Default()).Delete(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// MenuByRole .
// @router /api/admin/menu/role [GET]
func MenuByRole(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.Empty
	resp := new(admin.MenuInfoListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	roleIDInterface, exist := c.Get("roleID")
	if !exist || roleIDInterface == nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized")
		return
	}
	roleID, err := strconv.Atoi(roleIDInterface.(string))
	if err != nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized, roleID is not int")
		return
	}
	menuTree, total, err := logic.NewMenu(data.Default()).ListByRole(ctx, uint64(roleID))
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	var menuInfos []*admin.MenuInfo
	err = copier.Copy(&menuInfos, &menuTree)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	resp.Data = menuInfos
	resp.Total = total
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// MenuList .
// @router /api/admin/menu/list [GET]
func MenuList(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.PageInfoReq
	resp := new(admin.MenuInfoListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var listReq admin2.MenuListReq
	listReq.Page = req.Page
	listReq.PageSize = req.PageSize
	menuTree, total, err := logic.NewMenu(data.Default()).List(ctx, &listReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	var menuInfos []*admin.MenuInfo
	err = copier.Copy(&menuInfos, &menuTree)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	resp.Data = menuInfos
	resp.Total = uint64(total)
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// CreateMenuParam .
// @router /api/admin/menu/param/create [POST]
func CreateMenuParam(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateMenuParamReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var menuParamReq admin2.MenuParam
	err = copier.Copy(&menuParamReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	err = logic.NewMenu(data.Default()).CreateMenuParam(ctx, &menuParamReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateMenuParam .
// @router /api/admin/menu/param/update [POST]
func UpdateMenuParam(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateMenuParamReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var menuParamReq admin2.MenuParam
	err = copier.Copy(&menuParamReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	err = logic.NewMenu(data.Default()).UpdateMenuParam(ctx, &menuParamReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DeleteMenuParam .
// @router /api/admin/menu/param [DELETE]
func DeleteMenuParam(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewMenu(data.Default()).DeleteMenuParam(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// MenuParamListByMenuID .
// @router /api/admin/menu/param/list [GET]
func MenuParamListByMenuID(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(admin.MenuParamListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	menuParams, total, err := logic.NewMenu(data.Default()).MenuParamListByMenuID(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	var menuParamInfos []*admin.MenuParamResp
	err = copier.Copy(&menuParamInfos, &menuParams)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.Data = menuParamInfos
	resp.Total = total
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}
