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) TotpStatusGet(ctx context.Context, req *v1.TotpStatusGetReq) (res *v1.TotpStatusGetRes, err error) {
	res = &v1.TotpStatusGetRes{}
	userInfo, err := service.SysAuth().LoginOnlyLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	if userInfo.OtpSecret == "" {
		res.Status = false
		otp, err2 := mfa.GetOtp(userInfo.Id, userInfo.Username, userInfo.OtpKey, userInfo.OtpSecret)
		if err2 != nil {
			err = err2
			return
		}
		res.Image = otp.QrImage
		res.OtpSecret = otp.Secret
		res.OtpKey = otp.Key
	} else {
		res.Status = true
	}
	return
}
