// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package user

import (
	"context"
	"errors"
	"juwan-backend/app/users/rpc/usercenter"
	"juwan-backend/common/utils/contextj"

	"juwan-backend/app/users/api/internal/svc"
	"juwan-backend/app/users/api/internal/types"

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

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

// 切换当前激活角色
func NewSwitchRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SwitchRoleLogic {
	return &SwitchRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SwitchRoleLogic) SwitchRole(req *types.SwitchRoleReq) (resp *types.EmptyResp, err error) {
	id, err := contextj.UserIDFrom(l.ctx)
	if err != nil {
		logx.Errorf("get user id from context: %v", err)
		return nil, errors.New("illegal id")
	}
	_, err = l.svcCtx.UserRpc.SwitchRole(l.ctx, &usercenter.SwitchRoleReq{
		UserId:  id,
		NewRole: req.Role,
	})

	if err != nil {
		logx.Errorf("update user info by id: %v", err)
		return nil, errors.New("update user info by userid")
	}
	return
}
