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

// Test combined subject grading for student 6279
$student_id = 6279;
$session = 2025;
$exam_type = 'final';

echo "Testing combined subject grading for student $student_id...\n\n";

$stmt = $pdo->prepare("
    SELECT r.*, sub.name as subject_name
    FROM results r
    LEFT JOIN subjects sub ON r.subject_id = sub.id
    WHERE r.student_id = ? AND r.session = ? AND r.exam_type = ?
    AND r.id IN (
        SELECT MAX(id)
        FROM results
        WHERE student_id = ? AND session = ? AND exam_type = ?
        GROUP BY subject_id
    )
    ORDER BY sub.name
");
$stmt->execute([$student_id, $session, $exam_type, $student_id, $session, $exam_type]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Group subjects like in marksheet
$bangla_results = [];
$english_results = [];

foreach ($results as $result) {
    $subject_name = strtolower($result['subject_name']);
    if (stripos($result['subject_name'], 'বাংলা') !== false || stripos($result['subject_name'], 'Bangla') !== false) {
        $bangla_results[] = $result;
    } else if (stripos($result['subject_name'], 'ইংরেজি') !== false || stripos($result['subject_name'], 'English') !== false) {
        $english_results[] = $result;
    }
}

echo "Bangla subjects found: " . count($bangla_results) . "\n";
foreach ($bangla_results as $r) {
    echo "  - " . $r['subject_name'] . ": CQ=" . $r['cq_marks'] . ", MCQ=" . $r['mcq_marks'] . "\n";
}

echo "\nEnglish subjects found: " . count($english_results) . "\n";
foreach ($english_results as $r) {
    echo "  - " . $r['subject_name'] . ": CQ=" . $r['cq_marks'] . "\n";
}

// Test Bangla grading
if (!empty($bangla_results)) {
    $cq_marks_array = [];
    $mcq_total = 0;

    foreach ($bangla_results as $result) {
        $cq_marks_array[] = $result['cq_marks'];
        $mcq_total += $result['mcq_marks'];
    }

    $cq_average = count($cq_marks_array) > 0 ? array_sum($cq_marks_array) / count($cq_marks_array) : 0;
    $mcq_average = count($bangla_results) > 0 ? $mcq_total / count($bangla_results) : 0;

    echo "\nBangla combined grading:\n";
    echo "CQ marks: " . implode(', ', $cq_marks_array) . " (avg: $cq_average)\n";
    echo "MCQ average: $mcq_average\n";
    echo "Pass criteria: CQ > 22 AND MCQ > 9\n";

    if ($cq_average > 22 && $mcq_average > 9) {
        $overall_marks = $cq_average + $mcq_average;
        $percentage = ($overall_marks / 100) * 100; // Assuming 100 total marks

        if ($percentage >= 80) $grade = 'A+';
        elseif ($percentage >= 70) $grade = 'A';
        elseif ($percentage >= 60) $grade = 'A-';
        elseif ($percentage >= 50) $grade = 'B';
        elseif ($percentage >= 40) $grade = 'C';
        elseif ($percentage >= 33) $grade = 'D';
        else $grade = 'F';

        echo "Combined Bangla Grade: $grade (percentage: $percentage%)\n";
    } else {
        echo "Combined Bangla Grade: F (criteria not met)\n";
    }
}

// Test English grading
if (!empty($english_results)) {
    $cq_marks_array = [];

    foreach ($english_results as $result) {
        $cq_marks_array[] = $result['cq_marks'];
    }

    $cq_average = count($cq_marks_array) > 0 ? array_sum($cq_marks_array) / count($cq_marks_array) : 0;

    echo "\nEnglish combined grading:\n";
    echo "CQ marks: " . implode(', ', $cq_marks_array) . " (avg: $cq_average)\n";
    echo "Pass criteria: CQ > 32\n";

    if ($cq_average > 32) {
        $percentage = ($cq_average / 100) * 100; // Assuming 100 total marks

        if ($percentage >= 80) $grade = 'A+';
        elseif ($percentage >= 70) $grade = 'A';
        elseif ($percentage >= 60) $grade = 'A-';
        elseif ($percentage >= 50) $grade = 'B';
        elseif ($percentage >= 40) $grade = 'C';
        elseif ($percentage >= 33) $grade = 'D';
        else $grade = 'F';

        echo "Combined English Grade: $grade (percentage: $percentage%)\n";
    } else {
        echo "Combined English Grade: F (criteria not met)\n";
    }
}
?>
