<?php
// Test the fourth subject calculation logic
echo "Testing Fourth Subject Calculation Logic\n";
echo "========================================\n\n";

// Example from user: sub1=4, sub2=3, sub3=5, 4th sub=5
// Calculation should be: (4+3+5+3)/3=5 (where 3 is the adjusted 4th subject grade)

$grade_points = [4, 3, 5, 5]; // Four subjects
$fourth_subject_index = 3; // 4th subject (0-indexed)

echo "Original grade points: " . json_encode($grade_points) . "\n";

// Adjust the fourth subject grade point
$adjusted_grade_points = $grade_points;
if ($adjusted_grade_points[$fourth_subject_index] > 2.00) {
    $adjusted_grade_points[$fourth_subject_index] -= 2.00;
}

echo "After adjusting 4th subject (subtract 2 if > 2): " . json_encode($adjusted_grade_points) . "\n";

// Calculate total grade points
$total_grade_points = array_sum($adjusted_grade_points);
echo "Total grade points: " . $total_grade_points . "\n";

// According to the new logic, we divide by (number of subjects - 1) when there's a fourth subject
$gpa_subjects = count($grade_points); // 4 subjects
$gpa = $total_grade_points / ($gpa_subjects - 1); // Divide by 3

echo "GPA calculation: " . $total_grade_points . " / " . ($gpa_subjects - 1) . " = " . round($gpa, 2) . "\n";

echo "\nExpected result: 5.00\n";
echo "Actual result: " . round($gpa, 2) . "\n";

if (round($gpa, 2) == 5.00) {
    echo "✓ Calculation is CORRECT!\n";
} else {
    echo "✗ Calculation is INCORRECT!\n";
}
?>