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

try {
    // Check students with session "2025" in class_id 10 (Ten) and their group names
    $stmt = $pdo->prepare("SELECT id, first_name, last_name, roll_number, group_name FROM students WHERE class_id = ? AND session = ? AND status = 'active'");
    $stmt->execute([10, '2025']);
    $students = $stmt->fetchAll();
    
    echo "Students in class 10 (Ten) with session '2025':\n";
    foreach ($students as $student) {
        $group_display = ($student['group_name'] === null) ? 'NULL' : "'" . $student['group_name'] . "'";
        echo "- " . $student['first_name'] . " " . $student['last_name'] . " (Roll: " . $student['roll_number'] . ", Group: " . $group_display . ")\n";
    }
    
    // Test the exact query used in bulk_admit_card.php
    echo "\nTesting the exact query from bulk_admit_card.php:\n";
    $sql = "SELECT s.*, c.name as class_name, c.section 
            FROM students s 
            JOIN classes c ON s.class_id = c.id 
            WHERE s.status = 'active' 
            AND s.class_id = ? 
            AND s.session = ? 
            AND (s.group_name IS NULL OR s.group_name = '') 
            ORDER BY CAST(s.roll_number AS UNSIGNED), s.first_name";
    
    echo "SQL: " . $sql . "\n";
    echo "Params: [10, '2025']\n";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute([10, '2025']);
    $filtered_students = $stmt->fetchAll();
    
    echo "Results from filtered query: " . count($filtered_students) . "\n";
    
    if (count($filtered_students) > 0) {
        echo "First few students:\n";
        foreach (array_slice($filtered_students, 0, 5) as $student) {
            echo "- " . $student['first_name'] . " " . $student['last_name'] . " (Roll: " . $student['roll_number'] . ", Group: '" . ($student['group_name'] ?? 'NULL') . "')\n";
        }
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>