<?php

declare(strict_types=1);

$capturePath = getenv('WA_CAPTURE_PATH');
if (! is_string($capturePath) || $capturePath === '') {
    $capturePath = __DIR__.'/../../storage/app/qa/provider-e2e/whatsapp-capture.json';
}

$headers = function_exists('getallheaders') ? getallheaders() : [];
$body = file_get_contents('php://input');

@mkdir(dirname($capturePath), 0777, true);
file_put_contents($capturePath, json_encode([
    'time' => date(DATE_ATOM),
    'method' => $_SERVER['REQUEST_METHOD'] ?? null,
    'uri' => $_SERVER['REQUEST_URI'] ?? null,
    'headers' => $headers,
    'body' => $body,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

header('Content-Type: application/json');
echo json_encode(['ok' => true], JSON_UNESCAPED_SLASHES);
