<?php

namespace apexl\Io\modules\user\entities\operators;

use apexl\Io\operators\entityDatabaseOperator;

class twoFactorMethodOperator extends entityDatabaseOperator
{
    public function __construct($table, $primaryKey = 'id')
    {
        parent::__construct($table, $primaryKey);
    }

    public function getMostRecentPendingOfTypeForUser($type, $userId)
    {
        $entity = $this->vault
            ->select($this->dbTable)
            ->fields()
            ->where('type', $type)
            ->where('user_id', $userId)
            ->where('is_active', 0)
            ->orderBy('id', 'DESC')
            ->limit(1)
            ->execute()
            ->fetchAssoc();

        return ['updateEntityData' => true, 'data' => $entity];
    }

    public function getMostRecentActiveForUser($userId)
    {
        $entity = $this->vault
            ->select($this->dbTable)
            ->fields()
            ->where('user_id', $userId)
            ->where('is_active', 1)
            ->orderBy('id', 'DESC')
            ->limit(1)
            ->execute()
            ->fetchAssoc();

        return ['updateEntityData' => true, 'data' => $entity];
    }
}
