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

try {
    // Check if exams table exists and get its structure
    $stmt = $pdo->query("DESCRIBE exams");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h2>Exams Table Structure:</h2>";
    echo "<table border='1'>";
    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
    
    foreach ($columns as $column) {
        echo "<tr>";
        echo "<td>" . $column['Field'] . "</td>";
        echo "<td>" . $column['Type'] . "</td>";
        echo "<td>" . $column['Null'] . "</td>";
        echo "<td>" . $column['Key'] . "</td>";
        echo "<td>" . $column['Default'] . "</td>";
        echo "<td>" . $column['Extra'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Check some sample data
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM exams");
    $count = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "<p>Total exams in database: " . $count['count'] . "</p>";
    
    // Show some recent exams
    $stmt = $pdo->query("SELECT id, name, class_id, session, group_name FROM exams ORDER BY created_at DESC LIMIT 5");
    $exams = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>Recent Exams:</h3>";
    echo "<table border='1'>";
    echo "<tr><th>ID</th><th>Name</th><th>Class ID</th><th>Session</th><th>Group</th></tr>";
    
    foreach ($exams as $exam) {
        echo "<tr>";
        echo "<td>" . $exam['id'] . "</td>";
        echo "<td>" . $exam['name'] . "</td>";
        echo "<td>" . $exam['class_id'] . "</td>";
        echo "<td>" . $exam['session'] . "</td>";
        echo "<td>" . ($exam['group_name'] ?? '') . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Check classes table to verify class_id exists
    echo "<h3>Classes:</h3>";
    $stmt = $pdo->query("SELECT id, name, section FROM classes ORDER BY id LIMIT 10");
    $classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<table border='1'>";
    echo "<tr><th>ID</th><th>Name</th><th>Section</th></tr>";
    
    foreach ($classes as $class) {
        echo "<tr>";
        echo "<td>" . $class['id'] . "</td>";
        echo "<td>" . $class['name'] . "</td>";
        echo "<td>" . $class['section'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>