<?php
require_once __DIR__ . '/../../public/vendor/autoload.php';

use App\Controllers\PublicPaymentController;
use App\Models\Setting;

// Read raw payload from STDIN (we'll pipe it when running)
$raw = file_get_contents('php://stdin');
if (!$raw) {
    echo "No payload provided on STDIN. Provide JSON payload via stdin.\n";
    exit(1);
}

// Optionally compute signature if secret present
$setting = new Setting();
$secret = $setting->getValue('paystack_secret_key', '');
if ($secret) {
    $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] = hash_hmac('sha512', $raw, $secret);
}

// emulate web environment
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ?? 'localhost';

// place raw payload into php://input by using a temporary stream wrapper not needed when piping
// call controller
$controller = new PublicPaymentController();
// The controller reads php://input; since we piped input into this script, file_get_contents('php://input') inside controller will return the same data.
$controller->webhook(['gateway' => 'paystack']);
