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

// Get the test student's results
$stmt = $pdo->prepare("
    SELECT r.*, s.name as subject_name
    FROM results r
    LEFT JOIN subjects s ON r.subject_id = s.id
    WHERE r.student_id = ? AND r.session = ? AND r.exam_type = ?
    ORDER BY s.name
");
$stmt->execute([6185, '2025', 'midterm']);
$results = $stmt->fetchAll();

echo "Test Student Results:\n";
echo "Student ID: 6185\n";
echo "Session: 2025\n";
echo "Exam Type: midterm\n";
echo "----------------------------------------\n";

$total_grade_points = 0;
$passed_subjects = 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;
    }
}

foreach ($results as $result) {
    echo "Subject: " . ($result['subject_name'] ?? 'Unknown') . "\n";
    echo "  Marks: " . $result['marks_obtained'] . "\n";
    echo "  Grade: " . $result['grade'] . "\n";
    echo "  Point: " . getGradePoint($result['grade']) . "\n";
    
    if ($result['grade'] != 'F') {
        $passed_subjects++;
        $total_grade_points += getGradePoint($result['grade']);
    }
    
    echo "\n";
}

$gpa = ($passed_subjects > 0) ? round($total_grade_points / $passed_subjects, 2) : 0.00;

echo "----------------------------------------\n";
echo "Total Grade Points: " . $total_grade_points . "\n";
echo "Passed Subjects: " . $passed_subjects . "\n";
echo "Calculated GPA: " . $gpa . "\n";
?>