<?php
// Simulate being logged in as admin
session_start();
$_SESSION['user_id'] = 1;
$_SESSION['user_role'] = 'admin';

// Set the GET parameters
$_GET['session'] = '2025';
$_GET['class_id'] = '10';
$_GET['group_name'] = 'HUMANITIES';
$_GET['subject_id'] = '35';
$_GET['action'] = 'view';

// Now include the student_filter.php logic
ob_start();
include 'subjects/student_filter.php';
$content = ob_get_clean();

// Check if we got redirected to login (which would mean auth failed)
if (strpos($content, 'লগইন') !== false && strpos($content, 'username') !== false) {
    echo "ERROR: Got redirected to login page - authentication failed\n";
    exit(1);
}

// Check if we see the "No students found" message
if (strpos($content, 'কোনো শিক্ষার্থী পাওয়া যায়নি') !== false) {
    echo "RESULT: Page shows 'No students found' message\n";
} else if (strpos($content, 'শিক্ষার্থী:') !== false) {
    // Look for student count
    if (preg_match('/শিক্ষার্থী: (\d+)/', $content, $matches)) {
        echo "RESULT: Page shows " . $matches[1] . " students found\n";
    } else {
        echo "RESULT: Page shows students (found student count indicator)\n";
    }
} else {
    echo "RESULT: Could not determine if students were found or not\n";
    // Show beginning of content for debugging
    echo "\nFirst 500 characters of output:\n";
    echo substr($content, 0, 500) . "...\n";
}

// Also run our direct database test to compare
echo "\n" . str_repeat("=", 50) . "\n";
echo "DIRECT DATABASE TEST:\n";
require_once 'config/database.php';

$session_name = '2025';
$class_id = '10';
$group_name = 'HUMANITIES';
$subject_id = '35';

// Build WHERE clause exactly like student_filter.php
$where = ["s.status='active'"];
$params = [];
if ($session_name !== '') { $where[] = 's.session = ?'; $params[] = $session_name; }
if ($class_id !== '') { $where[] = 's.class_id = ?'; $params[] = $class_id; }
if ($group_name !== '') {
    $where[] = "COALESCE(s.group_name,'') = ?";
    $params[] = $group_name;
}
$where_sql = implode(' AND ', $where);

$students = [];
$sql = "SELECT s.id, s.roll_number, s.student_id, s.first_name, s.last_name,
               s.session, c.name AS class_name, c.section, COALESCE(s.group_name,'') AS group_name,
               sub.name AS subject_name, sub.code AS subject_code
        FROM students s
        JOIN classes c ON s.class_id = c.id
        JOIN student_subjects ss ON ss.student_id = s.id AND ss.subject_id = ?
        JOIN subjects sub ON sub.id = ss.subject_id
        WHERE $where_sql
        ORDER BY s.roll_number";
$stmt = $pdo->prepare($sql);
$stmt->execute(array_merge([$subject_id], $params));
$students = $stmt->fetchAll();

echo "Direct database query found: " . count($students) . " students\n";
?>