<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

$path = __DIR__ . '/../data/vapid_keys.json';
$raw = file_get_contents($path);
if ($raw === false) {
    fwrite(STDERR, "Unable to read {$path}\n");
    exit(1);
}

$j = json_decode($raw, true);
if (!is_array($j)) {
    fwrite(STDERR, "Invalid JSON in {$path}\n");
    exit(1);
}

$pub = (string)($j['publicKey'] ?? '');
$priv = (string)($j['privateKey'] ?? '');

echo "publicKey length: " . strlen($pub) . "\n";
echo "privateKey length: " . strlen($priv) . "\n";

try {
    \Base64Url\Base64Url::decode($pub);
    echo "publicKey: OK\n";
} catch (Throwable $e) {
    echo "publicKey: FAIL ({$e->getMessage()})\n";
}

try {
    \Base64Url\Base64Url::decode($priv);
    echo "privateKey: OK\n";
} catch (Throwable $e) {
    echo "privateKey: FAIL ({$e->getMessage()})\n";
}
