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

echo "<h1>🔍 Check Group Names</h1>";

// Check all students in class 11, session 2024
$stmt = $pdo->prepare("SELECT id, first_name, last_name, roll_number, group_name FROM students WHERE class_id = 11 AND session = '2024' AND status = 'active' ORDER BY id LIMIT 20");
$stmt->execute();
$students = $stmt->fetchAll();

echo "<h2>Students in Class 11, Session 2024:</h2>";
echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
echo "<tr style='background: #f8f9fa;'>";
echo "<th>ID</th><th>Name</th><th>Roll</th><th>Group Name</th>";
echo "</tr>";

foreach ($students as $student) {
    $group_display = $student['group_name'] ?? 'NULL';
    if ($group_display === '') $group_display = 'EMPTY STRING';
    
    echo "<tr>";
    echo "<td><strong>{$student['id']}</strong></td>";
    echo "<td>{$student['first_name']} {$student['last_name']}</td>";
    echo "<td>{$student['roll_number']}</td>";
    echo "<td style='background: " . (empty($student['group_name']) ? '#fff3cd' : '#d4edda') . ";'>$group_display</td>";
    echo "</tr>";
}
echo "</table>";

// Test the WHERE condition
echo "<h2>Test WHERE Conditions:</h2>";

// Test 1: Empty group condition
echo "<h3>1. Students with empty/null group_name:</h3>";
$stmt = $pdo->prepare("SELECT id, first_name, group_name FROM students WHERE class_id = 11 AND session = '2024' AND status = 'active' AND (group_name IS NULL OR group_name = '') ORDER BY id LIMIT 10");
$stmt->execute();
$empty_group_students = $stmt->fetchAll();

echo "<p>Found: " . count($empty_group_students) . " students</p>";
foreach ($empty_group_students as $student) {
    echo "<p>ID: {$student['id']}, Name: {$student['first_name']}, Group: '" . ($student['group_name'] ?? 'NULL') . "'</p>";
}

// Test 2: Non-empty group condition
echo "<h3>2. Students with non-empty group_name:</h3>";
$stmt = $pdo->prepare("SELECT id, first_name, group_name FROM students WHERE class_id = 11 AND session = '2024' AND status = 'active' AND group_name IS NOT NULL AND group_name != '' ORDER BY id LIMIT 10");
$stmt->execute();
$non_empty_group_students = $stmt->fetchAll();

echo "<p>Found: " . count($non_empty_group_students) . " students</p>";
foreach ($non_empty_group_students as $student) {
    echo "<p>ID: {$student['id']}, Name: {$student['first_name']}, Group: '{$student['group_name']}'</p>";
}

echo "<p><a href='marks/entry.php'>← Back to Marks Entry</a></p>";
?>