package sysUser

import (
	"context"

	"kami/internal/errHandler"
	"kami/internal/service"
	"kami/utility/mfa"

	"kami/api/sysUser/v1"

	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) TotpReset(ctx context.Context, req *v1.TotpResetReq) (res *v1.TotpResetRes, err error) {
	userService := service.SysUser()
	userInfo, err := service.SysAuth().LoginOnlyLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	if !mfa.ValidCode(req.Code, userInfo.OtpSecret) {
		return nil, errHandler.WrapError(ctx, errHandler.ErrLoginUserTotpError, err, "验证码错误")
	}
	err = userService.ResetTotp(ctx, userInfo)
	return
}
