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

// Replicate exactly what transposed_tabulation_sheet.php does
$class_id = $_GET['class_id'] ?? '';
$session = $_GET['session'] ?? '';
$exam_type = $_GET['exam_type'] ?? '';
$group_name = trim($_GET['group_name'] ?? '');

if ($class_id && $session && $exam_type) {
    // Fetch subjects for this class exactly as tabulation sheet does
    $stmt = $pdo->prepare("
        SELECT DISTINCT s.id, s.name, s.code 
        FROM results r
        JOIN subjects s ON r.subject_id = s.id
        JOIN students st ON r.student_id = st.id
        WHERE st.class_id = ? AND r.session = ? AND r.exam_type = ?
        " . ($group_name ? "AND st.group_name = ?" : "") . "
        ORDER BY s.name
    ");
    $params = [$class_id, $session, $exam_type];
    if ($group_name) $params[] = $group_name;
    $stmt->execute($params);
    $raw_subjects = $stmt->fetchAll();
    
    // Group Bangla and English subjects exactly as tabulation sheet does
    $subjects = groupSubjects($raw_subjects);
    
    echo "CLASS ID: $class_id\n";
    echo "SESSION: $session\n";
    echo "EXAM TYPE: $exam_type\n";
    echo "GROUP NAME: $group_name\n\n";
    
    echo "RAW SUBJECTS COUNT: " . count($raw_subjects) . "\n";
    echo "DISPLAYED SUBJECTS COUNT: " . count($subjects) . "\n\n";
    
    echo "RAW SUBJECTS:\n";
    foreach ($raw_subjects as $subject) {
        echo "  - {$subject['name']} ({$subject['code']})\n";
    }
    
    echo "\nDISPLAYED SUBJECTS:\n";
    foreach ($subjects as $subject) {
        echo "  - {$subject['name']} ({$subject['code']})";
        if (isset($subject['id']) && ($subject['id'] == 'combined_bangla' || $subject['id'] == 'combined_english')) {
            echo " [COMBINED]";
        }
        echo "\n";
    }
    
    // Function to group Bangla and English subjects (copied exactly from tabulation sheet)
    function groupSubjects($raw_subjects) {
        $grouped_subjects = [];
        $bangla_subjects = [];
        $english_subjects = [];
        
        foreach ($raw_subjects as $subject) {
            // Check if this is a Bangla subject (Bangla or Bangla Grammar)
            if (stripos($subject['name'], 'বাংলা') !== false || stripos($subject['name'], 'Bangla') !== false) {
                $bangla_subjects[] = $subject;
            }
            // Check if this is an English subject (English or English Grammar)
            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'
            ];
        } 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;
    }
} else {
    echo "Missing required parameters\n";
}
?>