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

// Copy the groupSubjects function
function groupSubjects($raw_subjects) {
    $grouped_subjects = [];
    $bangla_subjects = [];
    $english_subjects = [];
    
    foreach ($raw_subjects as $subject) {
        // More specific matching for Bangla subjects - only actual Bangla papers, not related subjects
        if (stripos($subject['name'], 'Bangla 1st Paper') !== false || stripos($subject['name'], 'Bangla 2nd Paper') !== false || 
            stripos($subject['name'], 'বাংলা ১ম পত্র') !== false || stripos($subject['name'], 'বাংলা ২য় পত্র') !== false) {
            $bangla_subjects[] = $subject;
        }
        // Check if this is an English subject (more specific matching)
        else if (stripos($subject['name'], 'English 1st Paper') !== false || stripos($subject['name'], 'English 2nd Paper') !== false || 
                 stripos($subject['name'], 'ইংরেজি ১ম পত্র') !== false || stripos($subject['name'], 'ইংরেজি ২য় পত্র') !== 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'
        ];
    } else if (count($bangla_subjects) == 1) {
        // Add single Bangla subject as is
        $grouped_subjects[] = $bangla_subjects[0];
    }
    
    // Add combined English subject only if there are multiple English subjects
    if (count($english_subjects) > 1) {
        $grouped_subjects[] = [
            'id' => 'combined_english',
            'name' => 'ইংরেজি(একত্রে)',
            'code' => 'CENG'
        ];
    } else if (count($english_subjects) == 1) {
        // Add single English subject as is
        $grouped_subjects[] = $english_subjects[0];
    }
    
    return $grouped_subjects;
}

// Check student information
$stmt = $pdo->prepare('SELECT * FROM students WHERE student_id = ?');
$stmt->execute(['20250008']);
$student = $stmt->fetch();

if ($student) {
    echo "Student: {$student['first_name']} (ID: {$student['student_id']})\n";
    
    // 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 = ?
        AND s.group_name = ?
        ORDER BY sub.name
    ");
    $stmt->execute([$student['class_id'], $student['group_name']]);
    $raw_subjects = $stmt->fetchAll();
    
    echo "\nRaw Subjects:\n";
    foreach ($raw_subjects as $sub) {
        echo "  {$sub['name']} (ID: {$sub['id']})\n";
    }
    
    // Group subjects
    $grouped_subjects = groupSubjects($raw_subjects);
    
    echo "\nGrouped Subjects:\n";
    foreach ($grouped_subjects as $sub) {
        echo "  {$sub['name']} (ID: {$sub['id']})\n";
    }
}
?>