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

header('Content-Type: application/json');

try {
    // Verificar mensajes en la BD
    $stmt = $pdo->query("SELECT id, chat_id, sender_id, content, type, created_at FROM messages ORDER BY id DESC LIMIT 20");
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Contar total
    $count_stmt = $pdo->query("SELECT COUNT(*) as total FROM messages");
    $total = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
    
    echo json_encode([
        'total_messages' => $total,
        'recent_messages' => $messages
    ], JSON_PRETTY_PRINT);
    
} catch (PDOException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}
?>
