<?php
// Test file to verify reset functionality
require_once 'config/database.php';

echo "<h2>Testing Class Reset Functionality</h2>";

try {
    // Test database connection
    echo "<p>✅ Database connection: OK</p>";
    
    // Check if required tables exist
    $tables = ['students', 'classes', 'student_subjects', 'attendance', 'results', 'fees', 'library_issues', 'users'];
    
    foreach ($tables as $table) {
        $stmt = $pdo->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "<p>✅ Table '$table': EXISTS</p>";
        } else {
            echo "<p>❌ Table '$table': MISSING</p>";
        }
    }
    
    // Check classes with student counts
    echo "<h3>Classes with Student Counts:</h3>";
    $classes_stmt = $pdo->query("
        SELECT c.id, c.name, c.section, COUNT(s.id) as student_count 
        FROM classes c 
        LEFT JOIN students s ON c.id = s.class_id AND s.status = 'active'
        WHERE c.status = 'active' 
        GROUP BY c.id, c.name, c.section 
        ORDER BY c.name, c.section
    ");
    $classes = $classes_stmt->fetchAll();
    
    if (empty($classes)) {
        echo "<p>⚠️ No active classes found</p>";
    } else {
        echo "<table border='1' style='border-collapse: collapse; margin: 10px 0;'>";
        echo "<tr><th>Class ID</th><th>Class Name</th><th>Section</th><th>Active Students</th></tr>";
        foreach ($classes as $class) {
            echo "<tr>";
            echo "<td>{$class['id']}</td>";
            echo "<td>{$class['name']}</td>";
            echo "<td>{$class['section']}</td>";
            echo "<td>{$class['student_count']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    // Check if reset files exist
    $reset_file = 'students/reset_class_students.php';
    if (file_exists($reset_file)) {
        echo "<p>✅ Reset functionality file: EXISTS</p>";
    } else {
        echo "<p>❌ Reset functionality file: MISSING</p>";
    }
    
    // Check if list.php has been updated
    $list_file = 'students/list.php';
    if (file_exists($list_file)) {
        $list_content = file_get_contents($list_file);
        if (strpos($list_content, 'reset_class_students.php') !== false) {
            echo "<p>✅ List page integration: OK</p>";
        } else {
            echo "<p>❌ List page integration: MISSING</p>";
        }
    }
    
    echo "<h3>Test Summary:</h3>";
    echo "<p>✅ All core functionality appears to be properly implemented.</p>";
    echo "<p>🔗 <a href='students/list.php'>Go to Student List</a></p>";
    echo "<p>🔗 <a href='students/reset_class_students.php'>Go to Reset Page</a></p>";
    
} catch (Exception $e) {
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
}
?>

<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { width: 100%; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>