<?php
/**
 * VAPID Key Generator for Mabanga Express
 * This script generates the Public and Private keys required for Web Push notifications.
 */

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

use Minishlink\WebPush\VAPID;

header('Content-Type: text/plain');

echo "--- Mabanga Express VAPID Key Generator ---\n\n";

// Fix for XAMPP on Windows: Point to the openssl.cnf file if not found
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $opensslConfig = 'C:/xampp/php/extras/ssl/openssl.cnf';
    if (file_exists($opensslConfig)) {
        putenv("OPENSSL_CONF=$opensslConfig");
    }
}

try {
    $keys = VAPID::createVapidKeys();

    echo "Successfully generated new VAPID keys:\n\n";
    echo "Public Key (Use this in rider.js):\n";
    echo $keys['publicKey'] . "\n\n";

    echo "Private Key (Use this in create_delivery.php - KEEP SECRET):\n";
    echo $keys['privateKey'] . "\n\n";

    echo "-------------------------------------------\n";
    echo "IMPORTANT: Update your 'YOUR_PUBLIC_VAPID_KEY_HERE' and 'YOUR_PRIVATE_VAPID_KEY_HERE' placeholders with these values.";

} catch (Exception $e) {
    echo "Error generating keys: " . $e->getMessage();
}