package base_user_info

import (
	"context"
	"kami/internal/dao"
	"kami/internal/model/entity"
	"kami/internal/service"
	"kami/utility/config"
	"kami/utility/mfa"
)

func init() {
	service.RegisterBaseUserInfo(New())
}

func New() *sBaseUserInfo {
	return &sBaseUserInfo{}
}

type sBaseUserInfo struct{}

func (i *sBaseUserInfo) TotpValidate(ctx context.Context, totpCode string) (isPass bool, err error) {
	data := entity.V1UserInfo{}
	err = dao.V1UserInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1UserInfo.Columns().Nick, "admin").Scan(&data)
	if err != nil || data.Id == 0 {
		return
	}
	if data.OtpSecret == "" {
		isPass = true
		return
	}
	isPass = mfa.ValidCode(totpCode, data.OtpSecret)
	return
}
