package User

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-core/rpc/types/core"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetUserInfoLogic) GetUserInfo() (resp *types.UserBaseIDInfoResp, err error) {
	user, err := l.svcCtx.CoreRpc.GetUserById(l.ctx,
		&core.UUIDReq{Id: l.ctx.Value("userId").(string)})
	if err != nil {
		return nil, err
	}
	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: user.GetDepartmentId()})
	if err != nil {
		return nil, err
	}

	departmentName := *departmentInfo.Name
	if departmentName == "department.managementDepartment" {
		departmentName = "冠客数字员工管理系统"
	}
	roleNameMap := map[string]string{
		"role.admin":               "管理员",
		"role.stuff":               "员工",
		"role.seller":              "销售",
		"role.member":              "会员",
		"role.changeStatusSuccess": "已成功修改角色状态",
		"role.changeStatusFailed":  "修改角色状态失败",
		"role.duplicateRoleValue":  "角色值重复",
		"role.userExists":          "请先删除该角色下的用户",
		"role.roleForbidden":       "您的角色已停用",
	}

	var roleNames []string
	for _, roleName := range user.GetRoleName() {
		roleNames = append(roleNames, roleNameMap[roleName])
	}

	return &types.UserBaseIDInfoResp{
		BaseDataInfo: types.BaseDataInfo{Msg: errormsg.Success},
		Data: types.UserBaseIDInfo{
			UUID:             user.Id,
			Username:         user.Username,
			Nickname:         user.Nickname,
			Avatar:           user.Avatar,
			HomePath:         user.HomePath,
			Description:      user.Description,
			DepartmentName:   departmentName,
			DepartmentRemark: *departmentInfo.Remark,
			RoleName:         roleNames,
		},
	}, nil
}
