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

package auth

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

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

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

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

// 重置密码
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
	return &ResetPasswordLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordReq) (resp *types.EmptyResp, err error) {
	requestId, err := contextj.RequestIdFrom(l.ctx)
	if err != nil {
		logx.Errorf("get request id from context failed, err:%v.", err)
		return nil, errors.New("bad request")
	}
	hashedPassword, err := pwdUtils.HashPassword(req.NewPassword)
	if err != nil {
		logx.Errorf("hash password failed, err:%v.", err)
		return nil, errors.New("bad password")
	}

	_, err = l.svcCtx.UserRpc.ResetPassword(l.ctx, &usercenter.ResetPasswordReq{
		NewPassword: hashedPassword,
		Email:       req.Email,
		RequestId:   requestId,
		Vcode:       req.Vcode,
	})
	if err != nil {
		logx.Errorf("reset password failed, err:%v.", err)
		return nil, errors.New("reset password failed")
	}
	return
}
