<?php

declare(strict_types=1);
define('BASE_PATH', dirname(__DIR__));
$testDatabase = (string) getenv('PET_SPA_TEST_DB');
if (!preg_match('/^pet_shop_e2e_[a-z0-9_]+$/', $testDatabase)) throw new RuntimeException('Refusing non-E2E database.');
require_once BASE_PATH . '/config/database.php';
require_once BASE_PATH . '/app/models/User.php';
require_once BASE_PATH . '/app/models/RememberToken.php';
$pdo = (new Database())->connect();
$suffix = bin2hex(random_bytes(4));
$userId = (new User())->createCustomer(['full_name' => 'Remember E2E', 'phone' => '06' . random_int(10000000, 99999999), 'email' => 'remember-' . $suffix . '@example.test', 'password_hash' => 'x']);
$tokens = new RememberToken(); $raw = $tokens->issue($userId); $_COOKIE[RememberToken::COOKIE_NAME] = $raw;
if (!preg_match('/^[a-f0-9]{64}$/', (string) $raw) || !$tokens->restoreFromCookie()) throw new RuntimeException('Remember login token could not be restored.');
$tokens->revokeAllForUser($userId);
$check = $pdo->prepare('SELECT COUNT(*) FROM remember_tokens WHERE user_id = ?'); $check->execute([$userId]);
if ((int) $check->fetchColumn() !== 0) throw new RuntimeException('Remember tokens were not revoked.');
echo "REMEMBER TOKEN E2E PASS: secure token restore and revocation\n";
