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

// Function to determine pass/fail status for Bengali subjects based on average of CQ and MCQ parts
function isBengaliSubjectPass($cq_marks, $mcq_marks, $cq_full_marks = 50, $mcq_full_marks = 50) {
    // Calculate average of CQ and MCQ parts
    $cq_percentage = ($cq_marks / $cq_full_marks) * 100;
    $mcq_percentage = ($mcq_marks / $mcq_full_marks) * 100;
    
    // Calculate average percentage
    $average_percentage = ($cq_percentage + $mcq_percentage) / 2;
    
    // For Bengali subjects: if average of CQ and MCQ parts is >= 40%, then pass
    // This means both parts average to at least 40% for the subject to be passed
    return $average_percentage >= 40;
}

// Function to determine grade for Bengali subjects based on average of CQ and MCQ parts
function getBengaliSubjectGrade($cq_marks, $mcq_marks, $cq_full_marks = 50, $mcq_full_marks = 50) {
    // Calculate average of CQ and MCQ parts
    $cq_percentage = ($cq_marks / $cq_full_marks) * 100;
    $mcq_percentage = ($mcq_marks / $mcq_full_marks) * 100;
    
    // Calculate average percentage
    $average_percentage = ($cq_percentage + $mcq_percentage) / 2;
    
    // Assign grade based on average percentage
    if ($average_percentage >= 80) return 'A+';
    if ($average_percentage >= 70) return 'A';
    if ($average_percentage >= 60) return 'A-';
    if ($average_percentage >= 50) return 'B';
    if ($average_percentage >= 40) return 'C';
    if ($average_percentage >= 33) return 'D';
    return 'F';
}

// Function to convert grade to 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;
    }
}

// Test the new functions
echo "Testing Bengali subject grade calculation...\n";

// Test case 1: Both CQ and MCQ parts pass (average >= 40%)
$cq_marks = 45;
$mcq_marks = 35;
$grade = getBengaliSubjectGrade($cq_marks, $mcq_marks);
$grade_point = getGradePoint($grade);
echo "Test 1 - CQ: $cq_marks, MCQ: $mcq_marks => Grade: $grade, GP: $grade_point (Should be pass)\n";

// Test case 2: Average below 40% (should fail)
$cq_marks = 25;
$mcq_marks = 10;
$grade = getBengaliSubjectGrade($cq_marks, $mcq_marks);
$grade_point = getGradePoint($grade);
echo "Test 2 - CQ: $cq_marks, MCQ: $mcq_marks => Grade: $grade, GP: $grade_point (Should be F)\n";

// Test case 3: High scores
$cq_marks = 48;
$mcq_marks = 47;
$grade = getBengaliSubjectGrade($cq_marks, $mcq_marks);
$grade_point = getGradePoint($grade);
echo "Test 3 - CQ: $cq_marks, MCQ: $mcq_marks => Grade: $grade, GP: $grade_point (Should be high grade)\n";

// Test case 4: Exactly 40% average
$cq_marks = 40;
$mcq_marks = 40;
$grade = getBengaliSubjectGrade($cq_marks, $mcq_marks);
$grade_point = getGradePoint($grade);
echo "Test 4 - CQ: $cq_marks, MCQ: $mcq_marks => Grade: $grade, GP: $grade_point (Should be C - border case)\n";

// Test case 5: Below 40% average
$cq_marks = 35;
$mcq_marks = 30;
$grade = getBengaliSubjectGrade($cq_marks, $mcq_marks);
$grade_point = getGradePoint($grade);
echo "Test 5 - CQ: $cq_marks, MCQ: $mcq_marks => Grade: $grade, GP: $grade_point (Should be F)\n";

echo "All tests completed.\n";
?>