<?php

echo "🧪 TESTANDO WEBHOOK MANUALMENTE\n\n";

// Simula um evento do SendGrid
$fakeEvent = [
    [
        'event' => 'open',
        'email' => 'teste@example.com',
        'timestamp' => time(),
        'sg_message_id' => 'fake-message-id-123',
        'receivable_id' => '1',
        'document_number' => 'TEST-123'
    ]
];

// Envia pro webhook
$ch = curl_init('https://mailsendercentre.newerasolutions.com.br/api/webhooks/sendgrid.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fakeEvent));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: {$httpCode}\n";
echo "Response: {$response}\n";

if ($httpCode == 200) {
    echo "\n✅ WEBHOOK FUNCIONANDO!\n";
} else {
    echo "\n❌ WEBHOOK COM PROBLEMA!\n";
}
