<?php
// Debug script to test grade calculation for Bangla and English subjects

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];
    }
    
    echo "Bangla Debug Info:\n";
    echo "- CQ Marks Array: " . json_encode($cq_marks_array) . "\n";
    echo "- CQ Average: $cq_average\n";
    echo "- MCQ Marks: $mcq_marks\n";
    echo "- Total Marks: $total_marks\n";
    echo "- Pass Marks: $pass_marks\n";
    
    // For Bangla: CQ average > 22 AND MCQ > 9 to pass
    if ($cq_average > 22 && $mcq_marks > 9) {
        // Calculate overall marks
        $overall_marks = $cq_average + $mcq_marks;
        
        // Calculate percentage based on total marks
        $percentage = ($overall_marks / $total_marks) * 100;
        echo "- Overall Marks: $overall_marks\n";
        echo "- Percentage: $percentage%\n";
        
        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';
    } else {
        echo "- FAILED CRITERIA (CQ > 22 AND MCQ > 9)\n";
        // If criteria not met, return F
        return 'F';
    }
}

function calculateEnglishGrade($cq_marks_array, $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];
    }
    
    echo "English Debug Info:\n";
    echo "- CQ Marks Array: " . json_encode($cq_marks_array) . "\n";
    echo "- CQ Average: $cq_average\n";
    echo "- Total Marks: $total_marks\n";
    echo "- Pass Marks: $pass_marks\n";
    
    // For English: CQ average > 32 to pass
    if ($cq_average > 32) {
        // Calculate overall marks (CQ average only for English as per requirements)
        $overall_marks = $cq_average;
        
        // Calculate percentage based on total marks
        $percentage = ($overall_marks / $total_marks) * 100;
        echo "- Overall Marks: $overall_marks\n";
        echo "- Percentage: $percentage%\n";
        
        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';
    } else {
        echo "- FAILED CRITERIA (CQ > 32)\n";
        // If criteria not met, return F
        return 'F';
    }
}

// Test case 1: Student with 73 marks in Bangla
echo "=== Test Case 1: Bangla Subject (73 marks) ===\n";
$cq_marks_bangla = [40, 35]; // Two CQ papers
$mcq_marks_bangla = 33;
$total_marks_bangla = 100;
$pass_marks_bangla = 33;

$bangla_grade = calculateBanglaGrade($cq_marks_bangla, $mcq_marks_bangla, $total_marks_bangla, $pass_marks_bangla);
echo "Bangla Grade Result: $bangla_grade\n\n";

// Test case 2: Student with 63 marks in English
echo "=== Test Case 2: English Subject (63 marks) ===\n";
$cq_marks_english = [35, 28]; // Two CQ papers
$total_marks_english = 100;
$pass_marks_english = 33;

$english_grade = calculateEnglishGrade($cq_marks_english, $total_marks_english, $pass_marks_english);
echo "English Grade Result: $english_grade\n\n";

// Test case 3: Edge case where student should fail in English
echo "=== Test Case 3: English Subject (Should Fail) ===\n";
$cq_marks_english_fail = [30, 30]; // Average = 30 (less than 32)
$english_grade_fail = calculateEnglishGrade($cq_marks_english_fail, $total_marks_english, $pass_marks_english);
echo "English Grade Result (Should Fail): $english_grade_fail\n\n";

// Test case 4: Edge case where student should pass in English
echo "=== Test Case 4: English Subject (Should Pass) ===\n";
$cq_marks_english_pass = [33, 33]; // Average = 33 (greater than 32)
$english_grade_pass = calculateEnglishGrade($cq_marks_english_pass, $total_marks_english, $pass_marks_english);
echo "English Grade Result (Should Pass): $english_grade_pass\n";
?>