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

// Test grading functions
echo "=== TESTING GRADING FUNCTIONS ===\n\n";

// Test calculateBanglaGrade
echo "Testing calculateBanglaGrade:\n";
$cq_marks_array = [45, 48]; // Two CQ marks
$mcq_marks = 12; // MCQ average
$total_marks = 100;
$pass_marks = 40;

$bangla_grade = calculateBanglaGrade($cq_marks_array, $mcq_marks, $total_marks, $pass_marks);
echo "CQ marks: " . implode(', ', $cq_marks_array) . "\n";
echo "MCQ average: $mcq_marks\n";
echo "Total marks: $total_marks\n";
echo "Bangla Grade: $bangla_grade\n\n";

// Test calculateEnglishGrade
echo "Testing calculateEnglishGrade:\n";
$cq_marks_array = [35, 38]; // Two CQ marks
$total_marks = 100;
$pass_marks = 40;

$english_grade = calculateEnglishGrade($cq_marks_array, $total_marks, $pass_marks);
echo "CQ marks: " . implode(', ', $cq_marks_array) . "\n";
echo "Total marks: $total_marks\n";
echo "English Grade: $english_grade\n\n";

// Test with student data
echo "=== TESTING WITH STUDENT DATA ===\n";
$student_id = 6279;
$session = '2025';
$exam_type = 'final';

// Get Bangla subjects
$stmt = $pdo->prepare("
    SELECT r.cq_marks, r.mcq_marks
    FROM results r
    JOIN subjects sub ON r.subject_id = sub.id
    WHERE r.student_id = ? AND r.session = ? AND r.exam_type = ?
    AND (sub.name LIKE '%বাংলা%' OR sub.name LIKE '%Bangla%')
    AND r.id IN (
        SELECT MAX(r2.id)
        FROM results r2
        WHERE r2.student_id = r.student_id AND r2.subject_id = r.subject_id AND r2.session = r.session AND r2.exam_type = r.exam_type
    )
    ORDER BY sub.name
");
$stmt->execute([$student_id, $session, $exam_type]);
$bangla_results = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (!empty($bangla_results)) {
    $cq_marks = array_column($bangla_results, 'cq_marks');
    $mcq_marks = array_column($bangla_results, 'mcq_marks');
    $mcq_average = array_sum($mcq_marks) / count($mcq_marks);

    echo "Student Bangla CQ marks: " . implode(', ', $cq_marks) . "\n";
    echo "Student Bangla MCQ marks: " . implode(', ', $mcq_marks) . " (avg: $mcq_average)\n";

    $student_bangla_grade = calculateBanglaGrade($cq_marks, $mcq_average, 100, 40);
    echo "Student Bangla Grade: $student_bangla_grade\n";
}

// Include the grading functions
function calculateBanglaGrade($cq_marks_array, $mcq_marks, $total_marks, $pass_marks) {
    // Calculate average of CQ marks
    $cq_average = 0;
    if (!empty($cq_marks_array)) {
        $cq_average = array_sum($cq_marks_array) / count($cq_marks_array);
    }

    // For Bangla: CQ average > 22 AND MCQ > 9 to pass
    if ($cq_average > 22 && $mcq_marks > 9) {
        // Calculate overall marks (CQ average + MCQ average)
        $overall_marks = $cq_average + $mcq_marks;

        // 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';
    } else {
        return 'F';
    }
}

function calculateEnglishGrade($cq_marks_array, $total_marks, $pass_marks) {
    // Calculate average of CQ marks
    $cq_average = 0;
    if (!empty($cq_marks_array)) {
        $cq_average = array_sum($cq_marks_array) / count($cq_marks_array);
    }

    // For English: CQ average > 32 to pass
    if ($cq_average > 32) {
        // Calculate overall marks (CQ average only for English)
        $overall_marks = $cq_average;

        // 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';
    } else {
        return 'F';
    }
}
?>
