<?php

declare(strict_types=1);

namespace BotGuard\Models;

use BotGuard\Core\Model;

/**
 * A pending verification session: the server-issued token + challenge answer,
 * created when /verify loads and resolved by /api/verify.
 */
final class VerificationSession extends Model
{
    protected string $table = 'verification_sessions';

    public function findByToken(string $token): ?array
    {
        return $this->firstWhere(['token' => $token]);
    }

    public function markCompleted(int $id): void
    {
        $this->updateById($id, ['status' => 'completed']);
    }

    /**
     * Housekeeping: expire stale pending sessions.
     */
    public function expireStale(): int
    {
        return $this->db->affectingStatement(
            "UPDATE `{$this->table}` SET `status` = 'expired' WHERE `status` = 'pending' AND `expires_at` < ?",
            [now()]
        );
    }
}
