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

$mid = $argv[1] ?? '';
if (!$mid) { echo "Usage: php find_webhook_by_mid.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) echo $row['created_at'] . ' | id:' . $row['id'] . PHP_EOL . $row['request_body'] . PHP_EOL;
    else echo "No encontrado\n";
} catch (Exception $e) {
    echo 'ERROR: '.$e->getMessage().PHP_EOL;
}
