package handler

import (
	"kra/internal/service"
	"kra/internal/service/dto"

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

type Menu struct{ service *service.MenuService }

func NewMenu(service *service.MenuService) *Menu { return &Menu{service: service} }

func (h *Menu) List(c *gin.Context) {
	items, err := h.service.Tree(c.Request.Context())
	if err != nil {
		Fail(c, "获取失败")
		return
	}
	Write(c, CodeSuccess, items, "获取成功")
}

func (h *Menu) Tree(c *gin.Context) {
	items, err := h.service.Tree(c.Request.Context())
	if err != nil {
		Fail(c, "获取失败")
		return
	}
	Write(c, CodeSuccess, gin.H{"menus": items}, "获取成功")
}

func (h *Menu) Create(c *gin.Context) {
	var req dto.MenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Path == "" {
		Fail(c, "Path值不能为空")
		return
	}
	if req.Name == "" {
		Fail(c, "Name值不能为空")
		return
	}
	if req.Component == "" {
		Fail(c, "Component值不能为空")
		return
	}
	if req.Sort < 0 {
		Fail(c, "Sort长度或值不在合法范围,ge=0")
		return
	}
	if req.Meta.Title == "" {
		Fail(c, "Title值不能为空")
		return
	}
	if err := h.service.Create(c.Request.Context(), &req); err != nil {
		Fail(c, "添加失败："+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "添加成功")
}

func (h *Menu) Update(c *gin.Context) {
	var req dto.MenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Path == "" {
		Fail(c, "Path值不能为空")
		return
	}
	if req.Name == "" {
		Fail(c, "Name值不能为空")
		return
	}
	if req.Component == "" {
		Fail(c, "Component值不能为空")
		return
	}
	if req.Sort < 0 {
		Fail(c, "Sort长度或值不在合法范围,ge=0")
		return
	}
	if req.Meta.Title == "" {
		Fail(c, "Title值不能为空")
		return
	}
	if err := h.service.Update(c.Request.Context(), &req); err != nil {
		Fail(c, "更新失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "更新成功")
}

func (h *Menu) Delete(c *gin.Context) {
	var req dto.DeleteMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "ID值不能为空")
		return
	}
	if err := h.service.Delete(c.Request.Context(), uint(req.ID)); err != nil {
		Fail(c, "删除失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "删除成功")
}

func (h *Menu) Find(c *gin.Context) {
	var req dto.GetMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "ID值不能为空")
		return
	}
	item, err := h.service.Find(c.Request.Context(), uint(req.ID))
	if err != nil {
		Fail(c, "获取失败")
		return
	}
	Write(c, CodeSuccess, gin.H{"menu": item}, "获取成功")
}

func (h *Menu) SetAuthorityMenus(c *gin.Context) {
	var req dto.SetAuthorityMenusRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.AuthorityID == 0 {
		Fail(c, "AuthorityId值不能为空")
		return
	}
	if err := h.service.SetAuthorityMenus(c.Request.Context(), &req); err != nil {
		Fail(c, "添加失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "添加成功")
}

func (h *Menu) AuthorityMenus(c *gin.Context) {
	var req dto.GetAuthorityMenusRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.AuthorityID == 0 {
		Fail(c, "AuthorityId值不能为空")
		return
	}
	menus, err := h.service.AuthorityMenus(c.Request.Context(), req.AuthorityID)
	if err != nil {
		Write(c, CodeError, gin.H{"menus": menus}, "获取失败")
		return
	}
	Write(c, CodeSuccess, gin.H{"menus": menus}, "获取成功")
}

func (h *Menu) RoleIDs(c *gin.Context) {
	var req dto.SetMenuRolesRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.MenuID == 0 {
		Fail(c, "菜单ID不能为空")
		return
	}
	ids, err := h.service.RoleIDs(c.Request.Context(), req.MenuID)
	if err != nil {
		Fail(c, "获取失败"+err.Error())
		return
	}
	if ids == nil {
		ids = []uint{}
	}
	defaultIDs, err := h.service.DefaultRouterRoleIDs(c.Request.Context(), req.MenuID)
	if err != nil {
		Fail(c, "获取失败"+err.Error())
		return
	}
	if defaultIDs == nil {
		defaultIDs = []uint{}
	}
	Write(c, CodeSuccess, gin.H{"authorityIds": ids, "defaultRouterAuthorityIds": defaultIDs}, "获取成功")
}

func (h *Menu) SetRoles(c *gin.Context) {
	var req dto.SetMenuRolesRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.MenuID == 0 {
		Fail(c, "菜单ID不能为空")
		return
	}
	if err := h.service.SetRoles(c.Request.Context(), &req); err != nil {
		Fail(c, "设置失败"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}
