// Code scaffolded by goctl. Safe to edit.
package member

import (
	"context"

	"gateway/internal/svc"
	"gateway/internal/types"

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

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

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

func (l *VerifyTOTPLogic) VerifyTOTP(req *types.TOTPVerifyReq) (*types.TOTPVerifyData, error) {
	actor, err := actorOrErr(l.ctx)
	if err != nil {
		return nil, err
	}
	if err := requireTOTP(l.svcCtx); err != nil {
		return nil, err
	}
	code := ""
	purposeRaw := ""
	if req != nil {
		code = req.Code
		purposeRaw = req.Purpose
	}
	purpose, err := parseStepUpPurpose(purposeRaw)
	if err != nil {
		return nil, err
	}
	if err := l.svcCtx.MemberTOTP.VerifyCode(l.ctx, actor.TenantID, actor.UID, code); err != nil {
		return nil, err
	}
	view, err := issueStepUpAfterVerify(l.ctx, l.svcCtx, actor.TenantID, actor.UID, purpose)
	if err != nil {
		return nil, err
	}
	return &types.TOTPVerifyData{
		StepUpToken: view.StepUpToken,
		ExpiresIn:   view.ExpiresIn,
	}, nil
}
