<?php
require_once 'config.php';

echo "<h2>Database Debug - All Tables</h2>";

// Check entries table
$result = mysqli_query($link, "SHOW TABLES LIKE 'entries'");
if (mysqli_num_rows($result) > 0) {
    echo "<h3>✅ Entries Table - Recent Entries:</h3>";
    
    $result = mysqli_query($link, "SELECT id, username, entry_time FROM entries ORDER BY entry_time DESC LIMIT 10");
    if ($result && mysqli_num_rows($result) > 0) {
        echo "<table border='1' style='border-collapse: collapse; margin: 10px 0;'>";
        echo "<tr><th>ID</th><th>Username</th><th>Entry Time</th></tr>";
        
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . htmlspecialchars($row['id']) . "</td>";
            echo "<td>" . htmlspecialchars($row['username']) . "</td>";
            echo "<td>" . htmlspecialchars($row['entry_time']) . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>No entries found</p>";
    }
} else {
    echo "<p>❌ Entries table does not exist</p>";
}

// Check results table  
$result = mysqli_query($link, "SHOW TABLES LIKE 'results'");
if (mysqli_num_rows($result) > 0) {
    echo "<h3>✅ Results Table - Recent Results:</h3>";
    
    $result = mysqli_query($link, "SELECT * FROM results ORDER BY created_at DESC LIMIT 5");
    if ($result && mysqli_num_rows($result) > 0) {
        echo "<table border='1' style='border-collapse: collapse; margin: 10px 0;'>";
        echo "<tr><th>ID</th><th>Date</th><th>Game</th><th>Result</th><th>Created</th></tr>";
        
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . htmlspecialchars($row['id']) . "</td>";
            echo "<td>" . htmlspecialchars($row['result_date']) . "</td>";
            echo "<td>" . htmlspecialchars($row['game']) . "</td>";
            echo "<td>" . htmlspecialchars($row['result_number']) . "</td>";
            echo "<td>" . htmlspecialchars($row['created_at']) . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>No results found</p>";
    }
} else {
    echo "<p>❌ Results table does not exist</p>";
}

mysqli_close($link);
?>