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

// Get a student from class 10
$stmt = $pdo->prepare("SELECT * FROM students WHERE class_id = 10 AND status = 'active' ORDER BY roll_number LIMIT 1");
$stmt->execute();
$student = $stmt->fetch();

if (!$student) {
    die("No student found in class 10");
}

echo "<h2>Student: " . $student['first_name'] . " " . $student['last_name'] . " (Roll: " . $student['roll_number'] . ")</h2>";

// Get all subjects assigned to this student
$stmt = $pdo->prepare("
    SELECT ss.subject_id, ss.is_fourth_subject, s.name as subject_name, s.code as subject_code 
    FROM student_subjects ss 
    JOIN subjects s ON ss.subject_id = s.id 
    WHERE ss.student_id = ? AND s.status = 'active' 
    ORDER BY s.name
");
$stmt->execute([$student['id']]);
$student_subjects = $stmt->fetchAll();

echo "<h3>Student's subjects (" . count($student_subjects) . "):</h3>";
echo "<ul>";
foreach ($student_subjects as $subject) {
    $is_fourth = $subject['is_fourth_subject'] ? " (4th)" : "";
    echo "<li>" . $subject['subject_name'] . " (" . $subject['subject_code'] . ") - ID: " . $subject['subject_id'] . $is_fourth . "</li>";
}

// Check if History of Bangladesh and Bangladesh and World Introduction are in the student's subjects
$history_found = false;
$bangladesh_world_found = false;
$ict_found = false;

foreach ($student_subjects as $subject) {
    if (strpos($subject['subject_name'], 'History of Bangladesh') !== false) {
        $history_found = true;
    }
    if (strpos($subject['subject_name'], 'Bangladesh and World Introduction') !== false) {
        $bangladesh_world_found = true;
    }
    if (strpos($subject['subject_name'], 'ICT') !== false) {
        $ict_found = true;
    }
}

echo "</ul>";
echo "<h3>Specific subjects check:</h3>";
echo "<p>History of Bangladesh found: " . ($history_found ? "Yes" : "No") . "</p>";
echo "<p>Bangladesh and World Introduction found: " . ($bangladesh_world_found ? "Yes" : "No") . "</p>";
echo "<p>ICT found: " . ($ict_found ? "Yes" : "No") . "</p>";

// Check results for this student
$stmt = $pdo->prepare("SELECT * FROM results WHERE student_id = ? AND session = '2025' AND exam_type = 'final'");
$stmt->execute([$student['id']]);
$results = $stmt->fetchAll();

echo "<h3>Student's results (" . count($results) . "):</h3>";
echo "<ul>";
foreach ($results as $result) {
    // Get subject name
    $stmt2 = $pdo->prepare("SELECT name, code FROM subjects WHERE id = ?");
    $stmt2->execute([$result['subject_id']]);
    $subject = $stmt2->fetch();
    
    echo "<li>" . ($subject ? $subject['name'] : "Unknown") . " - Marks: " . $result['marks_obtained'] . ", Grade: " . $result['grade'] . "</li>";
}
echo "</ul>";
?>