<?php
// debug_schema.php - Affiche la structure des tables pour trouver les bons noms de colonnes
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo "<h1>Debug Schema BDD</h1>";

// Logique de connexion robuste (Copie de print_feuille.php)
$configPath = __DIR__ . '/config/database.php';
$config = require $configPath;

try {
    $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset={$config['charset']}";
    $db = new \PDO($dsn, $config['user'], $config['pass'], [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
    ]);
    echo "<p style='color:green'>Connexion OK (Config)</p>";
} catch (\PDOException $e) {
    try {
        $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset={$config['charset']}";
        $db = new \PDO($dsn, 'root', '', [
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
        ]);
        echo "<p style='color:orange'>Connexion OK (Fallback Root)</p>";
    } catch (\PDOException $e2) {
        die("Erreur Connexion: " . $e2->getMessage());
    }
}

// Fonction pour afficher les colonnes d'une table
function describeTable($db, $tableName) {
    echo "<h2>Table: $tableName</h2>";
    try {
        $stmt = $db->query("DESCRIBE $tableName");
        $columns = $stmt->fetchAll();
        echo "<table border='1' style='border-collapse:collapse; width:100%'>";
        echo "<tr style='background:#eee'><th>Field</th><th>Type</th><th>Null</th></tr>";
        foreach ($columns as $col) {
            echo "<tr>";
            echo "<td><b>" . htmlspecialchars($col['Field']) . "</b></td>";
            echo "<td>" . htmlspecialchars($col['Type']) . "</td>";
            echo "<td>" . htmlspecialchars($col['Null']) . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } catch (Exception $e) {
        echo "<p style='color:red'>Erreur: " . $e->getMessage() . "</p>";
    }
}

// Analyser les tables clés
describeTable($db, 'Departs');
describeTable($db, 'Feuilles');
describeTable($db, 'BusGares');
describeTable($db, 'Tickets');

?>
