<?php
// Simulate the processing logic from the marksheet
require_once 'config/database.php';

$student_id = 6279;
$session = 2025;
$exam_type = 'final';

// Get student details
$stmt = $pdo->prepare("
    SELECT s.*, c.name as class_name, c.section
    FROM students s
    JOIN classes c ON s.class_id = c.id
    WHERE s.id = ?
");
$stmt->execute([$student_id]);
$student = $stmt->fetch();

// Get results for the student
$stmt = $pdo->prepare("
    SELECT
        COALESCE(r.id, NULL) as result_id,
        COALESCE(r.marks_obtained, 0) as marks_obtained,
        COALESCE(r.grade, 'F') as grade,
        ss.subject_id,
        sub.name as subject_name,
        ss.is_fourth_subject
    FROM student_subjects ss
    LEFT JOIN subjects sub ON ss.subject_id = sub.id
    LEFT JOIN (
        SELECT *
        FROM results
        WHERE student_id = ? AND session = ? AND exam_type = ?
        AND id IN (
            SELECT MAX(id)
            FROM results
            WHERE student_id = ? AND session = ? AND exam_type = ?
            GROUP BY subject_id
        )
    ) r ON ss.student_id = r.student_id AND ss.subject_id = r.subject_id
    WHERE ss.student_id = ? AND sub.status = 'active'
    ORDER BY CASE WHEN ss.is_fourth_subject = 1 THEN 1 ELSE 0 END, sub.name
");
$stmt->execute([$student_id, $session, $exam_type, $student_id, $session, $exam_type, $student_id]);
$results_with_marks = $stmt->fetchAll();

echo "Initial results_with_marks: " . count($results_with_marks) . "\n";

// Simulate the processing from the marksheet code
$results = [];
$has_missing_marks = false;

foreach ($results_with_marks as $result) {
    $result['cq_marks_array'] = !empty($result['cq_marks']) ? [$result['cq_marks']] : [];
    // ... processing logic ...
    $results[] = $result;
}

echo "After initial processing: " . count($results) . "\n";

// Now simulate the categorization
$bangla_results = [];
$english_results = [];
$science_math_subjects = [];
$middle_subjects = [];
$ict_economics_subjects = [];
$career_edu = null;
$agri_edu = null;

foreach ($results as $result) {
    $subject_name = strtolower($result['subject_name']);

    // Check if this is a Bangla subject
    if (stripos($result['subject_name'], 'বাংলা') !== false || stripos($result['subject_name'], 'Bangla') !== false) {
        $bangla_results[] = $result;
    }
    // Check if this is an English subject
    else if (stripos($result['subject_name'], 'ইংরেজি') !== false || stripos($result['subject_name'], 'English') !== false) {
        $english_results[] = $result;
    }
    // Check for Science and Math subjects
    else if (strpos($subject_name, 'science') !== false || strpos($subject_name, 'বিজ্ঞান') !== false ||
             strpos($subject_name, 'math') !== false || strpos($subject_name, 'গণিত') !== false) {
        $science_math_subjects[] = $result;
    }
    // Check for ICT and Economics
    else if (strpos($subject_name, 'ict') !== false || strpos($subject_name, 'আইসিটি') !== false ||
             strpos($subject_name, 'economics') !== false || strpos($subject_name, 'অর্থনীতি') !== false) {
        $ict_economics_subjects[] = $result;
    }
    // Check for Career Education
    else if (strpos($subject_name, 'career') !== false || strpos($subject_name, 'ক্যারিয়ার') !== false) {
        $career_edu = $result;
    }
    // Check for Agricultural Education
    else if (strpos($subject_name, 'agricultural') !== false || strpos($subject_name, 'কৃষি') !== false) {
        $agri_edu = $result;
    }
    // All other subjects go to middle
    else {
        $middle_subjects[] = $result;
    }
}

echo "Categorization:\n";
echo "  Bangla: " . count($bangla_results) . "\n";
echo "  English: " . count($english_results) . "\n";
echo "  Science/Math: " . count($science_math_subjects) . "\n";
echo "  Middle: " . count($middle_subjects) . "\n";
echo "  ICT/Economics: " . count($ict_economics_subjects) . "\n";
echo "  Career Edu: " . ($career_edu ? 1 : 0) . "\n";
echo "  Agri Edu: " . ($agri_edu ? 1 : 0) . "\n";

// Build top subjects - show individual subjects instead of combining
$top_subjects = [];

// Add individual Bangla subjects
if (!empty($bangla_results)) {
    echo "Adding individual Bangla subjects...\n";
    $top_subjects = array_merge($top_subjects, $bangla_results);
}

// Add individual English subjects
if (!empty($english_results)) {
    echo "Adding individual English subjects...\n";
    $top_subjects = array_merge($top_subjects, $english_results);
}

echo "Top subjects after combining: " . count($top_subjects) . "\n";

// Build combined middle
$combined_middle = array_merge($science_math_subjects, $middle_subjects, $ict_economics_subjects);

if ($career_edu !== null) {
    $combined_middle[] = $career_edu;
}

if ($agri_edu !== null) {
    $combined_middle[] = $agri_edu;
}

echo "Combined middle subjects: " . count($combined_middle) . "\n";

// Final results
$final_results = array_merge($top_subjects, $combined_middle);

echo "Final results count: " . count($final_results) . "\n";

if (!empty($final_results)) {
    echo "Final results subjects:\n";
    foreach ($final_results as $result) {
        echo "  - {$result['subject_name']}\n";
    }
} else {
    echo "ERROR: Final results is empty!\n";
}
?>
