package sysUser

import (
	"context"

	"github.com/jinzhu/copier"

	v1 "kami/api/sysUser/v1"
	sysUserLoginV1 "kami/api/sys_user_login/v1"
	"kami/internal/model"
	"kami/internal/service"
	"kami/utility/token"
)

func (c *ControllerV1) UserMenusGet(ctx context.Context, req *v1.UserMenusGetReq) (res *v1.UserMenusGetRes, err error) {
	userToken, _ := token.ParseUserToken(ctx, req.Authorization)
	//
	menuList, permissions, err := service.SysUser().GetAdminRules(ctx, userToken.UserID)
	user, err := service.SysUser().GetAdminUserByUsernamePassword(ctx, &model.UserLoginInput{
		Username: userToken.Username,
	})
	if err != nil {
		return
	}
	menuAPIList := make([]*sysUserLoginV1.UserMenus, len(menuList))
	userAPIInfo := v1.LoginUserRecord{}
	_ = copier.CopyWithOption(&menuAPIList, &menuList, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	_ = copier.CopyWithOption(&user, &userAPIInfo, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	res = &v1.UserMenusGetRes{
		MenuList:    menuAPIList,
		Permissions: permissions,
		UserInfo:    &userAPIInfo,
	}
	return
}
