<?php
// Standalone test for grading functions without database dependencies

// Custom function to calculate Bangla subject grade based on SSC criteria
// Average of 2 CQ marks + MCQ marks must individually pass for overall pass
function calculateBanglaGrade($cq_marks_array, $mcq_marks, $total_marks, $pass_marks) {
    // Calculate average of 2 CQ marks
    $cq_average = 0;
    if (count($cq_marks_array) >= 2) {
        $cq_average = ($cq_marks_array[0] + $cq_marks_array[1]) / 2;
    } else if (count($cq_marks_array) == 1) {
        $cq_average = $cq_marks_array[0];
    }
    
    // Calculate overall marks (average of CQ + MCQ)
    $overall_marks = $cq_average + $mcq_marks;
    
    // Check if both CQ average and MCQ pass individually
    // For SSC-like system, both CQ average and MCQ must be >= 33
    $cq_pass_marks = 33;
    $mcq_pass_marks = 33;
    
    // For SSC-like system, both CQ average and MCQ must pass
    if ($cq_average < $cq_pass_marks || $mcq_marks < $mcq_pass_marks) {
        return 'F';
    }
    
    // Also check overall marks
    if ($overall_marks < $pass_marks) {
        return 'F';
    }
    
    // Calculate percentage based on total marks
    $percentage = ($overall_marks / $total_marks) * 100;
    
    if ($percentage >= 80) return 'A+';
    elseif ($percentage >= 70) return 'A';
    elseif ($percentage >= 60) return 'A-';
    elseif ($percentage >= 50) return 'B';
    elseif ($percentage >= 40) return 'C';
    elseif ($percentage >= 33) return 'D';
    else return 'F';
}

// Custom function to calculate English subject grade based on SSC criteria
// Both English and English Grammar require average of 2 CQ marks >= 33
function calculateEnglishGrade($cq_marks_array, $total_marks, $pass_marks) {
    // Calculate average of CQ marks (requirement is average of 2 CQ >= 33)
    $cq_average = 0;
    if (count($cq_marks_array) >= 2) {
        $cq_average = ($cq_marks_array[0] + $cq_marks_array[1]) / 2;
    } else if (count($cq_marks_array) == 1) {
        $cq_average = $cq_marks_array[0];
    }
    
    // For English subjects, both need average of 2 CQ marks >= 33
    $cq_pass_marks = 33; // Fixed requirement: average >= 33
    
    if ($cq_average < $cq_pass_marks) {
        return 'F';
    }
    
    // Also check overall marks
    if ($cq_average < $pass_marks) {
        return 'F';
    }
    
    // Calculate percentage based on total marks
    $percentage = ($cq_average / $total_marks) * 100;
    
    if ($percentage >= 80) return 'A+';
    elseif ($percentage >= 70) return 'A';
    elseif ($percentage >= 60) return 'A-';
    elseif ($percentage >= 50) return 'B';
    elseif ($percentage >= 40) return 'C';
    elseif ($percentage >= 33) return 'D';
    else return 'F';
}

echo "<h2>Testing Bangla and English Grading Functions</h2>\n";

// Test Bangla grading function
echo "<h3>Bangla Grading Tests:</h3>\n";

// Test case 1: Pass - Both CQ average and MCQ >= 33
$cq_marks = [40, 35]; // Average = 37.5
$mcq_marks = 25;
$total_marks = 100;
$pass_marks = 33;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 1 - CQ: [40, 35], MCQ: 25 => Grade: $result<br>\n";
echo "Debug - CQ Average: 37.5, MCQ: 25, Overall: 62.5<br>\n";

// Test case 2: Fail - CQ average < 33
$cq_marks = [30, 25]; // Average = 27.5
$mcq_marks = 30;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 2 - CQ: [30, 25], MCQ: 30 => Grade: $result (Expected: F)<br>\n";
echo "Debug - CQ Average: 27.5, MCQ: 30<br>\n";

// Test case 3: Fail - MCQ < 33
$cq_marks = [40, 35]; // Average = 37.5
$mcq_marks = 20;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 3 - CQ: [40, 35], MCQ: 20 => Grade: $result (Expected: F)<br>\n";
echo "Debug - CQ Average: 37.5, MCQ: 20<br>\n";

// Test case 4: Pass with good grade
$cq_marks = [45, 40]; // Average = 42.5
$mcq_marks = 40;
$result = calculateBanglaGrade($cq_marks, $mcq_marks, $total_marks, $pass_marks);
echo "Test 4 - CQ: [45, 40], MCQ: 40 => Grade: $result<br>\n";
echo "Debug - CQ Average: 42.5, MCQ: 40, Overall: 82.5<br>\n";

echo "<br>\n";

// Test English grading function
echo "<h3>English Grading Tests:</h3>\n";

// Test case 1: Pass - CQ average >= 33
$cq_marks = [35, 31]; // Average = 33
$total_marks = 100;
$pass_marks = 33;
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 1 - CQ: [35, 31] => Grade: $result<br>\n";
echo "Debug - CQ Average: 33<br>\n";

// Test case 2: Fail - CQ average < 33
$cq_marks = [30, 32]; // Average = 31
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 2 - CQ: [30, 32] => Grade: $result (Expected: F)<br>\n";
echo "Debug - CQ Average: 31<br>\n";

// Test case 3: Pass with good grade
$cq_marks = [40, 36]; // Average = 38
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 3 - CQ: [40, 36] => Grade: $result<br>\n";
echo "Debug - CQ Average: 38<br>\n";

// Test case 4: Single CQ mark
$cq_marks = [35]; // Average = 35
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 4 - CQ: [35] => Grade: $result<br>\n";
echo "Debug - CQ Average: 35<br>\n";

echo "<br>\n";

// Test English Grammar (should use same logic as English)
echo "<h3>English Grammar Grading Tests:</h3>\n";

// Test case 1: Pass - CQ average >= 33
$cq_marks = [34, 32]; // Average = 33
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 1 - CQ: [34, 32] => Grade: $result<br>\n";
echo "Debug - CQ Average: 33<br>\n";

// Test case 2: Fail - CQ average < 33
$cq_marks = [30, 30]; // Average = 30
$result = calculateEnglishGrade($cq_marks, $total_marks, $pass_marks);
echo "Test 2 - CQ: [30, 30] => Grade: $result (Expected: F)<br>\n";
echo "Debug - CQ Average: 30<br>\n";
?>