<?php
require_once __DIR__ . '/../config/config_enhanced.php';
require_once __DIR__ . '/../classes/Database.php';

$mid = $argv[1] ?? '';
if (!$mid) { echo "Usage: php dump_webhook_payload.php <message_id>\n"; exit(1); }

try {
    $db = Database::getInstance();
    $row = $db->fetch('SELECT id, request_body, created_at FROM webhook_logs WHERE request_body LIKE ? ORDER BY created_at DESC LIMIT 1', ['%'.$mid.'%']);
    if ($row) {
        $file = __DIR__ . '/webhook_payload_'.$mid.'.json';
        file_put_contents($file, $row['request_body']);
        echo "Saved payload to: $file\n";
    } else echo "No encontrado\n";
} catch (Exception $e) {
    echo 'ERROR: '.$e->getMessage().PHP_EOL;
}
