<?php
session_start();
require_once 'config/database.php';

// Simple debugging script to check Class 10 subject enrollment
echo "<h2>Debugging Class 10 Subject Enrollment</h2>\n";

$class_id = 10;
$session = date('Y');
$exam_type = 'final';

// Get students in this class
$stmt = $pdo->prepare("
    SELECT s.* FROM students s 
    WHERE s.class_id = ? AND s.status = 'active'
    ORDER BY s.student_id
");
$stmt->execute([$class_id]);
$students = $stmt->fetchAll();

echo "<h3>Students in Class 10:</h3>\n";
echo "<p>Total students: " . count($students) . "</p>\n";

if (empty($students)) {
    echo "<p>No students found in Class 10.</p>\n";
    exit();
}

// Get all subjects for students in this class
$stmt = $pdo->prepare("
    SELECT DISTINCT sub.id, sub.name, sub.code 
    FROM student_subjects ss
    JOIN subjects sub ON ss.subject_id = sub.id
    JOIN students s ON ss.student_id = s.id
    WHERE s.class_id = ?
    ORDER BY sub.name
");
$stmt->execute([$class_id]);
$raw_subjects = $stmt->fetchAll();

echo "<h3>Raw Subjects from student_subjects table:</h3>\n";
echo "<p>Total raw subjects: " . count($raw_subjects) . "</p>\n";

if (!empty($raw_subjects)) {
    echo "<ul>\n";
    foreach ($raw_subjects as $subject) {
        echo "<li>" . htmlspecialchars($subject['name']) . " (ID: " . $subject['id'] . ", Code: " . htmlspecialchars($subject['code']) . ")</li>\n";
    }
    echo "</ul>\n";
}

// Group Bangla and English subjects (simplified version of the function)
function groupSubjectsDebug($raw_subjects) {
    $grouped_subjects = [];
    $bangla_subjects = [];
    $english_subjects = [];
    
    foreach ($raw_subjects as $subject) {
        // Check if this is a Bangla subject
        if (stripos($subject['name'], 'বাংলা') !== false || stripos($subject['name'], 'Bangla') !== false) {
            $bangla_subjects[] = $subject;
        }
        // Check if this is an English subject
        else if (stripos($subject['name'], 'ইংরেজি') !== false || stripos($subject['name'], 'English') !== false) {
            $english_subjects[] = $subject;
        }
        // For other subjects, add directly
        else {
            $grouped_subjects[] = $subject;
        }
    }
    
    // Add combined Bangla subject only if there are multiple Bangla subjects
    if (count($bangla_subjects) > 1) {
        $grouped_subjects[] = [
            'id' => 'combined_bangla',
            'name' => 'বাংলা(একত্রে)',
            'code' => 'CBAN'
        ];
        echo "<p>Combined Bangla subjects: " . count($bangla_subjects) . " subjects combined</p>\n";
    } else if (count($bangla_subjects) == 1) {
        // Add single Bangla subject as is
        $grouped_subjects[] = $bangla_subjects[0];
        echo "<p>Single Bangla subject: " . htmlspecialchars($bangla_subjects[0]['name']) . "</p>\n";
    }
    
    // Add combined English subject only if there are multiple English subjects
    if (count($english_subjects) > 1) {
        $grouped_subjects[] = [
            'id' => 'combined_english',
            'name' => 'ইংরেজি(একত্রে)',
            'code' => 'CENG'
        ];
        echo "<p>Combined English subjects: " . count($english_subjects) . " subjects combined</p>\n";
    } else if (count($english_subjects) == 1) {
        // Add single English subject as is
        $grouped_subjects[] = $english_subjects[0];
        echo "<p>Single English subject: " . htmlspecialchars($english_subjects[0]['name']) . "</p>\n";
    }
    
    return $grouped_subjects;
}

// Group subjects
$subjects = groupSubjectsDebug($raw_subjects);

echo "<h3>Grouped Subjects:</h3>\n";
echo "<p>Total grouped subjects: " . count($subjects) . "</p>\n";

if (!empty($subjects)) {
    echo "<ul>\n";
    foreach ($subjects as $subject) {
        echo "<li>" . htmlspecialchars($subject['name']) . " (ID: " . $subject['id'] . ", Code: " . htmlspecialchars($subject['code']) . ")</li>\n";
    }
    echo "</ul>\n";
}

// Check subject enrollment for each student
echo "<h3>Subject Enrollment Per Student:</h3>\n";
echo "<table border='1' style='border-collapse: collapse;'>\n";
echo "<tr><th>Roll</th><th>Name</th><th>Total Subjects Enrolled</th><th>Enrolled Subjects</th></tr>\n";

foreach ($students as $student) {
    // Get subjects for this student
    $stmt = $pdo->prepare("
        SELECT sub.id, sub.name, sub.code
        FROM student_subjects ss
        JOIN subjects sub ON ss.subject_id = sub.id
        WHERE ss.student_id = ?
        ORDER BY sub.name
    ");
    $stmt->execute([$student['id']]);
    $student_subjects = $stmt->fetchAll();
    
    $subject_names = [];
    foreach ($student_subjects as $subj) {
        $subject_names[] = $subj['name'] . " (" . $subj['code'] . ")";
    }
    
    echo "<tr>\n";
    echo "<td>" . htmlspecialchars($student['student_id']) . "</td>\n";
    echo "<td>" . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . "</td>\n";
    echo "<td>" . count($student_subjects) . "</td>\n";
    echo "<td>" . implode(", ", $subject_names) . "</td>\n";
    echo "</tr>\n";
}

echo "</table>\n";

echo "<p><a href='reports/tabulation_sheet.php?class_id=10&session=$session&exam_type=$exam_type'>View Tabulation Sheet</a></p>\n";
?>