<?php

declare(strict_types=1);

$coinbaseAutoload = dirname(__DIR__) . '/application/third_party/coinbase/autoload.php';
if (is_file($coinbaseAutoload)) {
    require $coinbaseAutoload;
}
require dirname(__DIR__) . '/application/third_party/webpush/vendor/autoload.php';

use Minishlink\WebPush\Subscription;
use Minishlink\WebPush\WebPush;

$publicKey = 'BBoy80TJSvERvrNtzBDUszdwtcYYAR_ZWY_bkks3RECMlUSkQi7-B9ta1_rV4BXhkqOZaVUKhHoNsWiAvo4XeZc';
$privateKey = '04pshPSU5nmy9Pa7VucHMvb5gSsZzNa-CjruEgJau_U';

$webPush = new WebPush(array(
    'VAPID' => array(
        'subject' => 'https://pluscash.live',
        'publicKey' => $publicKey,
        'privateKey' => $privateKey
    )
), array('TTL' => 60, 'urgency' => 'high'), 1, array(
    'connect_timeout' => 1,
    'verify' => false
));

$subscription = Subscription::create(array(
    'endpoint' => 'https://127.0.0.1:9/subscription',
    'keys' => array(
        'p256dh' => $publicKey,
        'auth' => 'AAAAAAAAAAAAAAAAAAAAAA'
    ),
    'contentEncoding' => 'aes128gcm'
));

$report = $webPush->sendOneNotification($subscription, json_encode(array(
    'title' => 'PlusCash',
    'body' => 'Runtime smoke test'
)));

if ($report->getEndpoint() !== 'https://127.0.0.1:9/subscription') {
    throw new RuntimeException('The Web Push report returned an unexpected endpoint.');
}
if ($report->isSuccess()) {
    throw new RuntimeException('The local closed endpoint unexpectedly accepted the test message.');
}

fwrite(STDOUT, "PASS: Web Push runtime encrypted, signed and prepared a notification.\n");
