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

try {
    // Find students who have subjects for exam ID 68
    $stmt = $pdo->prepare("
        SELECT DISTINCT s.id, s.first_name, s.last_name, s.roll_number
        FROM students s
        JOIN student_subjects ss ON s.id = ss.student_id
        JOIN exam_subjects es ON ss.subject_id = es.subject_id
        WHERE es.exam_id = ? AND s.class_id = 10 AND s.session = '2025'
        ORDER BY s.roll_number
        LIMIT 10
    ");
    $stmt->execute([68]);
    $students = $stmt->fetchAll();
    
    echo "Students with subjects for exam ID 68:\n";
    foreach ($students as $student) {
        echo "- ID: " . $student['id'] . ", Name: " . $student['first_name'] . " " . $student['last_name'] . " (Roll: " . $student['roll_number'] . ")\n";
    }
    
    if (!empty($students)) {
        // Test with the first student
        $student_id = $students[0]['id'];
        echo "\nTesting with student ID $student_id:\n";
        
        $stmt = $pdo->prepare("
            SELECT es.*, s.name as subject_name, s.code as subject_code
            FROM exam_subjects es
            JOIN subjects s ON es.subject_id = s.id
            JOIN student_subjects ss ON es.subject_id = ss.subject_id
            WHERE es.exam_id = ? AND ss.student_id = ?
            ORDER BY es.exam_date, es.start_time
        ");
        $stmt->execute([68, $student_id]);
        $subjects = $stmt->fetchAll();
        
        echo "Found " . count($subjects) . " subjects:\n";
        foreach ($subjects as $subject) {
            echo "- " . $subject['subject_name'] . " (" . $subject['subject_code'] . ")\n";
            echo "  Date: " . $subject['exam_date'] . ", Time: " . $subject['start_time'] . " - " . $subject['end_time'] . "\n";
        }
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>