<?php

namespace apexl\Io\modules\user\entities;

use apexl\Config\Singleton;
use apexl\encryption\Encrypt;
use apexl\Io\includes\Entity;
use apexl\Io\modules\user\entities\operators\refreshTokenOperator;
use apexl\Io\modules\user\entities\operators\twoFactorMethodOperator;
use apexl\Io\modules\user\entities\operators\userNoteOperator;
use apexl\Io\operators\entityDatabaseOperator;

class twoFactorMethodEntity extends Entity
{
    public function __construct()
    {
        parent::__construct('user_two_factor_methods');
        $this->setOperator(new twoFactorMethodOperator('user_two_factor_methods', 'id'));
        $this->config = Singleton::getInstance();
        $this->encryptionService = new Encrypt();
    }

    public function getSecret()
    {
        if (isset($this->id)) {
            $this->encryptionService = new Encrypt();
            return $this->encryptionService->decrypt($this->secret, $this->secret_iv, $this->config->app->encryption->key);

        }

        return null;
    }

    public function isLocked()
    {
        if (isset($this->locked_until)) {
            $lockedUntil = \DateTime::createFromFormat('Y-m-d H:i:s', $this->locked_until);
            $now = new \DateTime('now');
            if ($lockedUntil && $lockedUntil > $now) {
                return true;
            }
        }

        return false;
    }
}
