<?php
require_once 'config/config.php';

try {
    $db = Database::getInstance();
    echo "=== ESTRUCTURA TABLA CONVERSATIONS ===\n";
    
    $result = $db->query("DESCRIBE conversations");
    while($row = $result->fetch()) {
        echo $row['Field'] . " - " . $row['Type'] . "\n";
    }
    
    echo "\n=== CONSULTA VENTANA 24H PARA USER_ID=2 ===\n";
    $stmt = $db->query(
        "SELECT id, content, direction, created_at FROM conversations 
         WHERE user_id = 2 AND direction = 'incoming' 
         ORDER BY created_at DESC LIMIT 5"
    );
    
    $conversations = $stmt->fetchAll();
    if ($conversations) {
        foreach ($conversations as $msg) {
            echo "ID: {$msg['id']}, Contenido: {$msg['content']}, Fecha: {$msg['created_at']}\n";
        }
    } else {
        echo "No hay mensajes entrantes para user_id=2\n";
        
        // Insertar mensaje simple
        echo "\nInsertando mensaje entrante...\n";
        $db->query(
            "INSERT INTO conversations (user_id, content, direction, message_type, status) 
             VALUES (2, 'Mensaje de prueba', 'incoming', 'text', 'received')"
        );
        echo "✅ Mensaje insertado correctamente\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Trace: " . $e->getTraceAsString() . "\n";
}
?>