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

// Vérifier la structure de la table events
$query = "PRAGMA table_info(events)";
$columns = fetchAll($query);

echo "<div style='font-family: Arial, sans-serif; padding: 20px;'>";
echo "<h1>Structure de la table 'events'</h1>";

echo "<h2>Colonnes</h2>";
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>";
echo "<tr style='background-color: #f2f2f2;'><th>ID</th><th>Nom</th><th>Type</th><th>Not Null</th><th>Valeur par défaut</th><th>Clé primaire</th></tr>";

foreach ($columns as $column) {
    echo "<tr>";
    echo "<td>{$column['cid']}</td>";
    echo "<td>{$column['name']}</td>";
    echo "<td>{$column['type']}</td>";
    echo "<td>" . ($column['notnull'] ? 'Oui' : 'Non') . "</td>";
    echo "<td>" . (isset($column['dflt_value']) ? $column['dflt_value'] : 'NULL') . "</td>";
    echo "<td>" . ($column['pk'] ? 'Oui' : 'Non') . "</td>";
    echo "</tr>";
}

echo "</table>";

// Vérifier les données dans la table events
$query = "SELECT * FROM events ORDER BY id ASC";
$events = fetchAll($query);

echo "<h2>Données (Événements existants)</h2>";

if (count($events) > 0) {
    echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>";
    echo "<tr style='background-color: #f2f2f2;'><th>ID</th><th>Titre</th><th>Date de début</th><th>Date de fin</th><th>Statut</th></tr>";
    
    foreach ($events as $event) {
        echo "<tr>";
        echo "<td>{$event['id']}</td>";
        echo "<td>{$event['title']}</td>";
        echo "<td>{$event['start_date']}</td>";
        echo "<td>" . (isset($event['end_date']) ? $event['end_date'] : 'N/A') . "</td>";
        echo "<td>{$event['status']}</td>";
        echo "</tr>";
    }
    
    echo "</table>";
    
    echo "<p><strong>Nombre total d'événements :</strong> " . count($events) . "</p>";
} else {
    echo "<p>Aucun événement trouvé dans la table.</p>";
}

// Vérifier les requêtes SQL utilisées dans index.php
echo "<h2>Requête SQL utilisée dans index.php</h2>";
echo "<pre>";
$query = "SELECT events.*, categories.name as category_name, 
          (SELECT COUNT(*) FROM event_participants WHERE event_id = events.id) as participant_count 
          FROM events 
          LEFT JOIN categories ON events.category_id = categories.id 
          WHERE 1=1
          ORDER BY events.start_date DESC, events.start_time DESC";
echo htmlspecialchars($query);
echo "</pre>";

echo "<p><a href='../events/index.php' style='display: inline-block; padding: 10px 15px; background-color: #007bff; color: white; text-decoration: none; border-radius: 4px;'>Retour à la liste des événements</a></p>";

echo "</div>";
?>
