<?php
// Replicate EXACTLY the parameters from the URL
$_GET['session'] = '2025';
$_GET['class_id'] = '10';
$_GET['group_name'] = 'HUMANITIES';
$_GET['subject_id'] = '35';
$_GET['exam_type'] = '';  // This was in your URL
$_GET['action'] = 'view';

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

require_once 'config/database.php';

// Replicate the EXACT logic from student_filter.php
$session_name = $_GET['session'] ?? '';
$class_id     = $_GET['class_id'] ?? '';
$group_name   = $_GET['group_name'] ?? '';
$subject_id   = $_GET['subject_id'] ?? '';
$exam_type    = $_GET['exam_type'] ?? '';  // This was in your URL
$action       = $_GET['action'] ?? 'view';

echo "EXACT parameters from URL:\n";
echo "- session_name: '$session_name'\n";
echo "- class_id: '$class_id'\n";
echo "- group_name: '$group_name'\n";
echo "- subject_id: '$subject_id'\n";
echo "- exam_type: '$exam_type'\n";
echo "- action: '$action'\n\n";

// 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 !== '') {
    // Treat "General" and "সাধারণ" as same group in filter
    if (in_array($group_name, ['General','সাধারণ'], true)) {
        $where[] = "COALESCE(s.group_name,'') IN (?, ?, ?)";
        $params[] = 'General';
        $params[] = 'সাধারণ';
        $params[] = '';
    } else {
        $where[] = "COALESCE(s.group_name,'') = ?";
        $params[] = $group_name;
    }
}
$where_sql = implode(' AND ', $where);

echo "WHERE clause: $where_sql\n";
echo "Parameters: " . json_encode($params) . "\n\n";

$students = [];
if ($action === 'view' && $subject_id !== '' && $class_id !== '' && $session_name !== '') {
    echo "Executing student query...\n";
    $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";
    echo "SQL: $sql\n";
    $stmt = $pdo->prepare($sql);
    $all_params = array_merge([$subject_id], $params);
    echo "All parameters: " . json_encode($all_params) . "\n";
    
    try {
        $stmt->execute($all_params);
        $students = $stmt->fetchAll();
        echo "\nSUCCESS! Found " . count($students) . " students\n";
        
        if (!empty($students)) {
            echo "\nFirst 3 students:\n";
            for ($i = 0; $i < min(3, count($students)); $i++) {
                $s = $students[$i];
                echo "- {$s['roll_number']}. {$s['first_name']} {$s['last_name']} (ID: {$s['student_id']})\n";
            }
        }
    } catch (Exception $e) {
        echo "ERROR: " . $e->getMessage() . "\n";
    }
} else {
    echo "Conditions not met:\n";
    echo "- action === 'view': " . ($action === 'view' ? 'YES' : 'NO') . "\n";
    echo "- subject_id !== '': " . ($subject_id !== '' ? 'YES' : 'NO') . " ('$subject_id')\n";
    echo "- class_id !== '': " . ($class_id !== '' ? 'YES' : 'NO') . " ('$class_id')\n";
    echo "- session_name !== '': " . ($session_name !== '' ? 'YES' : 'NO') . " ('$session_name')\n";
}
?>