<?php
// Calculate Bangla grade based on the user's specific example
// CQ: 60, 67 (average: 63) and MCQ: 21, 20 (average: 20)

function calculateBanglaGrade($cq_marks_array, $mcq_marks, $total_marks = 100, $pass_marks = 33) {
    // Calculate average of 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];
    }
    
    echo "CQ marks: " . implode(", ", $cq_marks_array) . " (average: " . round($cq_average, 2) . ")\n";
    echo "MCQ marks: " . $mcq_marks . "\n";
    
    // For Bangla: CQ average > 22 AND MCQ > 9 to pass
    if ($cq_average > 22 && $mcq_marks > 9) {
        echo "Passing criteria met: CQ average > 22 AND MCQ > 9\n";
        
        // Calculate overall marks
        $overall_marks = $cq_average + $mcq_marks;
        echo "Overall marks (CQ average + MCQ): " . round($cq_average, 2) . " + " . $mcq_marks . " = " . round($overall_marks, 2) . "\n";
        
        // Calculate percentage based on total marks
        $percentage = ($overall_marks / $total_marks) * 100;
        echo "Percentage: (" . round($overall_marks, 2) . " / " . $total_marks . ") * 100 = " . round($percentage, 2) . "%\n";
        
        // Determine grade based on percentage
        if ($percentage >= 80) {
            $grade = 'A+';
            $gpa = 5.00;
        } elseif ($percentage >= 70) {
            $grade = 'A';
            $gpa = 4.00;
        } elseif ($percentage >= 60) {
            $grade = 'A-';
            $gpa = 3.50;
        } elseif ($percentage >= 50) {
            $grade = 'B';
            $gpa = 3.00;
        } elseif ($percentage >= 40) {
            $grade = 'C';
            $gpa = 2.00;
        } elseif ($percentage >= 33) {
            $grade = 'D';
            $gpa = 1.00;
        } else {
            $grade = 'F';
            $gpa = 0.00;
        }
        
        echo "Grade: " . $grade . "\n";
        echo "GPA: " . $gpa . "\n";
        
        return [
            'grade' => $grade,
            'gpa' => $gpa,
            'percentage' => $percentage,
            'overall_marks' => $overall_marks
        ];
    } else {
        echo "Failed passing criteria: CQ average <= 22 OR MCQ <= 9\n";
        echo "Grade: F\n";
        echo "GPA: 0.00\n";
        
        return [
            'grade' => 'F',
            'gpa' => 0.00,
            'percentage' => 0,
            'overall_marks' => 0
        ];
    }
}

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;
    }
}

// User's specific example
echo "=== Bangla Combined Subject Calculation ===\n";
echo "Input: বাংলা(একত্রে) 60, 67 (গড়: 63) 21, 20 (গড়: 20)\n\n";

$cq_marks = [60, 67];  // Two CQ marks
$mcq_marks = 20;       // Average of MCQ marks

echo "Calculation:\n";
$result = calculateBanglaGrade($cq_marks, $mcq_marks, 100, 33);

echo "\n=== Final Result ===\n";
echo "Subject: বাংলা(একত্রে)\n";
echo "CQ Marks: " . implode(", ", $cq_marks) . " (Average: " . round(($cq_marks[0] + $cq_marks[1]) / 2, 2) . ")\n";
echo "MCQ Marks: " . $mcq_marks . "\n";
echo "Overall Marks: " . round($result['overall_marks'], 2) . "\n";
echo "Percentage: " . round($result['percentage'], 2) . "%\n";
echo "Grade: " . $result['grade'] . "\n";
echo "GPA: " . $result['gpa'] . "\n";

// Additional verification
echo "\n=== Verification ===\n";
echo "CQ Average: (" . $cq_marks[0] . " + " . $cq_marks[1] . ") / 2 = " . (($cq_marks[0] + $cq_marks[1]) / 2) . "\n";
echo "CQ Average > 22: " . ((($cq_marks[0] + $cq_marks[1]) / 2) > 22 ? "Yes" : "No") . "\n";
echo "MCQ > 9: " . ($mcq_marks > 9 ? "Yes" : "No") . "\n";
echo "Passing criteria met: " . ((($cq_marks[0] + $cq_marks[1]) / 2) > 22 && $mcq_marks > 9 ? "Yes" : "No") . "\n";