<?php
// Test script to verify the corrected Bangla calculation

// Function to get grade point
function getGradePoint($grade) {
    switch ($grade) {
        case 'A+': return 5.00;
        case 'A': return 4.00;
        case 'A-': return 3.50;
        case 'B': return 3.00;
        case 'C': return 2.00;
        case 'D': return 1.00;
        default: return 0.00;
    }
}

echo "=== BANGLA CALCULATION VERIFICATION ===\n\n";

// Test the user's example: CQ: 60, 67 (avg: 63), MCQ: 21, 20 (avg: 20)
$cq_marks_array = [60, 67];
$mcq_marks_array = [21, 20];

// Calculate averages
$cq_average = array_sum($cq_marks_array) / count($cq_marks_array); // 63.5
$mcq_average = array_sum($mcq_marks_array) / count($mcq_marks_array); // 20.5

echo "Input:\n";
echo "- CQ Marks: " . implode(", ", $cq_marks_array) . " (Average: $cq_average)\n";
echo "- MCQ Marks: " . implode(", ", $mcq_marks_array) . " (Average: $mcq_average)\n";

// Check passing criteria (Jessore Board rules)
echo "\nProcessing:\n";
if ($cq_average > 22 && $mcq_average > 9) {
    echo "- Passed criteria: CQ > 22 AND MCQ > 9\n";
    
    // Calculate total obtained
    $total_obtained = $cq_average + $mcq_average; // 63.5 + 20.5 = 84
    echo "- Total obtained marks: $total_obtained\n";
    
    // Test with different possible full marks values to see which would give Grade B (3.00 GPA)
    echo "\nTesting different full marks scenarios:\n";
    
    $test_full_marks = [100, 140, 150, 160, 168, 170, 200];
    foreach ($test_full_marks as $full_marks) {
        $percentage = ($total_obtained / $full_marks) * 100;
        
        // Determine grade based on percentage
        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';
        
        $gpa = getGradePoint($grade);
        
        $status = ($grade == 'B' && $gpa == 3.00) ? " <-- MATCHES USER'S EXAMPLE!" : "";
        echo "- Full marks: $full_marks, Percentage: " . number_format($percentage, 2) . "%, Grade: $grade, GPA: $gpa$status\n";
    }
    
    echo "\nConclusion:\n";
    echo "- For the given CQ/MCQ marks (60,67 and 21,20), to get Grade B (3.00 GPA),\n";
    echo "  the total full marks would need to be approximately 140-168.\n";
    echo "- With standard 200 full marks (2 papers of 100 each), result would be Grade C (2.00 GPA).\n";
    echo "- The fix ensures proper calculation based on actual full marks from the system.\n";
    
} else {
    echo "- Failed criteria: CQ <= 22 OR MCQ <= 9\n";
    $grade = 'F';
    $gpa = getGradePoint($grade);
    echo "- Grade: $grade\n";
    echo "- GPA: $gpa\n";
}

echo "\n=== FIX SUMMARY ===\n";
echo "1. Removed hardcoded grade assignment that was forcing A+ grade\n";
echo "2. Fixed undefined variable error in grade calculation\n";
echo "3. Implemented proper percentage-based grading for combined subjects\n";
echo "4. Used actual full marks from the system instead of hardcoded values\n";
echo "5. Ensured marks_obtained reflects calculated values for proper display\n";
?>