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

echo "<h2>Debugging Results Matrix for Class 10</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 "<p>Total students: " . count($students) . "</p>\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 = ?
    ORDER BY sub.name
");
$stmt->execute([$class_id]);
$raw_subjects = $stmt->fetchAll();

echo "<p>Total raw subjects: " . count($raw_subjects) . "</p>\n";

// Group Bangla and English subjects
function groupSubjects($raw_subjects) {
    $grouped_subjects = [];
    $bangla_subjects = [];
    $english_subjects = [];
    
    foreach ($raw_subjects as $subject) {
        if (stripos($subject['name'], 'বাংলা') !== false || stripos($subject['name'], 'Bangla') !== false) {
            $bangla_subjects[] = $subject;
        } else if (stripos($subject['name'], 'ইংরেজি') !== false || stripos($subject['name'], 'English') !== false) {
            $english_subjects[] = $subject;
        } else {
            $grouped_subjects[] = $subject;
        }
    }
    
    if (count($bangla_subjects) > 1) {
        $grouped_subjects[] = [
            'id' => 'combined_bangla',
            'name' => 'বাংলা(একত্রে)',
            'code' => 'CBAN'
        ];
    } else if (count($bangla_subjects) == 1) {
        $grouped_subjects[] = $bangla_subjects[0];
    }
    
    if (count($english_subjects) > 1) {
        $grouped_subjects[] = [
            'id' => 'combined_english',
            'name' => 'ইংরেজি(একত্রে)',
            'code' => 'CENG'
        ];
    } else if (count($english_subjects) == 1) {
        $grouped_subjects[] = $english_subjects[0];
    }
    
    return $grouped_subjects;
}

$subjects = groupSubjects($raw_subjects);
echo "<p>Total grouped subjects: " . count($subjects) . "</p>\n";

// Get all results for this class, session, and exam
$stmt = $pdo->prepare("
    SELECT r.*, s.student_id as roll_number
    FROM results r
    JOIN students s ON r.student_id = s.id
    WHERE s.class_id = ? AND r.session = ? AND r.exam_type = ?
");
$result_params = [$class_id, $session, $exam_type];
$stmt->execute($result_params);
$all_results = $stmt->fetchAll();

echo "<p>Total results found: " . count($all_results) . "</p>\n";

// Organize results in matrix format
$results_matrix = [];
foreach ($all_results as $result) {
    $results_matrix[$result['student_id']][$result['subject_id']] = $result;
}

echo "<p>Results matrix keys (student IDs): " . count($results_matrix) . "</p>\n";

// Check first few students
$counter = 0;
foreach ($students as $student) {
    if ($counter >= 5) break; // Only check first 5 students
    
    echo "<h3>Student: " . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . " (ID: " . $student['id'] . ", Roll: " . $student['student_id'] . ")</h3>\n";
    
    // Check if student has results
    if (isset($results_matrix[$student['id']])) {
        echo "<p>Has results: " . count($results_matrix[$student['id']]) . " subjects</p>\n";
        
        // Show subject results
        foreach ($results_matrix[$student['id']] as $subject_id => $result) {
            echo "<p>Subject ID: $subject_id, Marks: " . $result['marks_obtained'] . "</p>\n";
        }
    } else {
        echo "<p>No results found for this student</p>\n";
    }
    
    $counter++;
}

// Check subject enrollment for first few students
echo "<h3>Subject Enrollment Check</h3>\n";
$counter = 0;
foreach ($students as $student) {
    if ($counter >= 3) break;
    
    echo "<h4>Student: " . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . "</h4>\n";
    
    // 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();
    
    echo "<p>Enrolled subjects: " . count($student_subjects) . "</p>\n";
    
    foreach ($student_subjects as $subj) {
        $has_result = isset($results_matrix[$student['id']][$subj['id']]) ? "YES" : "NO";
        echo "<p>" . htmlspecialchars($subj['name']) . " (ID: " . $subj['id'] . ") - Result: $has_result</p>\n";
    }
    
    $counter++;
}
?>