<?php

// Capture headers
$headers = getallheaders();

// Capture body
$body = file_get_contents('php://input');

// Build debug response
$debug = [
    'time' => date('c'),
    'headers' => $headers,
    'body_raw' => $body,
    'body_json' => json_decode($body, true)
];

// Save to file (persistent log)
file_put_contents(
    __DIR__ . '/webhook_debug.log',
    json_encode($debug, JSON_PRETTY_PRINT) . "\n\n",
    FILE_APPEND
);

// Return response to Cloudflare
header('Content-Type: application/json');
http_response_code(200);

echo json_encode([
    'received' => true,
    'debug' => $debug
], JSON_PRETTY_PRINT);