<?php
/**
 * Sudanna Pay — Test Webhook (اختبار الـ webhook)
 * POST /api/partners/webhook_test.php
 */
require_once __DIR__.'/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__.'/../../engine/Logger.php';
require_once __DIR__.'/../../engine/IntegrationEngine.php';
if($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit;}
$ctx = Kernel::handle(['auth' => true]);
$u   = $ctx['user'];
$b = json_decode(file_get_contents('php://input'),true)??[];
$webhookUrl = trim($b['webhook_url']??'');
if(!$webhookUrl){http_response_code(400);die(json_encode(['error'=>'webhook_url required']));}

$testPayload = [
    'event'             => 'payment.test',
    'payment_id'        => 'PAY_TEST_'.date('YmdHis'),
    'partner_reference' => 'TEST-001',
    'amount_sdg'        => 100,
    'status'            => 'completed',
    'completed_at'      => date('c'),
    'message'           => 'هذا اختبار webhook من Sudanna Pay',
];

$sent = IntegrationEngine::sendWebhook($webhookUrl, $testPayload, 'test_secret');
echo json_encode(['ok'=>$sent,'payload_sent'=>$testPayload,'webhook_url'=>$webhookUrl], JSON_UNESCAPED_UNICODE);
