<?php
// Test script to check subject grouping logic
require_once 'config/database.php';

// Test parameters from the user's URL
$class_id = 10;
$group_name = 'HUMANITIES';
$session = 2025;
$exam_type = 'final';

// Get all subjects for students in this class and group
$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([$class_id, $group_name]);
$raw_subjects = $stmt->fetchAll();

echo "Raw Subjects:\n";
foreach ($raw_subjects as $subject) {
    echo "- " . $subject['name'] . " (ID: " . $subject['id'] . ")\n";
}

// Apply the same grouping logic as in tabulation_sheet.php
$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;
        echo "Identified as Bangla subject: " . $subject['name'] . "\n";
    }
    // 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;
        echo "Identified as English subject: " . $subject['name'] . "\n";
    }
    // For other subjects, add directly
    else {
        $grouped_subjects[] = $subject;
    }
}

echo "\nBangla subjects count: " . count($bangla_subjects) . "\n";
echo "English subjects count: " . count($english_subjects) . "\n";

// Add combined Bangla subject only if there are multiple Bangla subjects
if (count($bangla_subjects) > 1) {
    echo "Creating combined Bangla subject\n";
    $grouped_subjects[] = [
        'id' => 'combined_bangla',
        'name' => 'বাংলা(একত্রে)',
        'code' => 'CBAN'
    ];
} else if (count($bangla_subjects) == 1) {
    echo "Adding single Bangla subject\n";
    $grouped_subjects[] = $bangla_subjects[0];
} else {
    echo "No Bangla subjects to add\n";
}

// Add combined English subject only if there are multiple English subjects
if (count($english_subjects) > 1) {
    echo "Creating combined English subject\n";
    $grouped_subjects[] = [
        'id' => 'combined_english',
        'name' => 'ইংরেজি(একত্রে)',
        'code' => 'CENG'
    ];
} else if (count($english_subjects) == 1) {
    echo "Adding single English subject\n";
    $grouped_subjects[] = $english_subjects[0];
} else {
    echo "No English subjects to add\n";
}

echo "\nFinal grouped subjects:\n";
foreach ($grouped_subjects as $subject) {
    echo "- " . $subject['name'] . " (ID: " . $subject['id'] . ")\n";
}
?>