<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\MorphTo;

class Google2FA extends Model
{
    protected $casts = [
        'status' => 'boolean',
    ];

    public function subscriber(): MorphTo
    {
        return $this->morphTo();
    }

    /**
     * Notes   : 更新密钥
     *
     * @Date   : 2022/11/30 20:51
     * @Author : <Jason.C>
     */
    public function upgrade()
    {
        $google2fa = app('pragmarx.google2fa');

        $this->secret = $google2fa->generateSecretKey(32);
        $this->save();
    }

    /**
     * Notes   : 验证
     *
     * @Date   : 2022/11/30 20:49
     * @Author : <Jason.C>
     * @param  string  $value
     * @return bool
     */
    public function verify(string $value): bool
    {
        $google2fa = app('pragmarx.google2fa');
        return $google2fa->verifyGoogle2FA($this->secret, $value);
    }
}
