<?php
include 'includes/conn.php';
include 'includes/functions.php';
requireRole('student');

$student_id = $_SESSION['user_id'];

// Fetch subjects with latest exam and test scores
$stmt = $pdo->prepare("
    SELECT s.name AS subject_id, g.first_note, g.first_ass, g.first_test, g.second_note, g.second_ass, g.second_test, g.exam, g.total
    FROM grades g
    JOIN subjects s ON g.subject_id = s.id
    WHERE g.student_id = ?
    ORDER BY s.name ASC
");
$stmt->execute([$student_id]);
$scores = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Return JSON data
header('Content-Type: application/json');
echo json_encode($scores);
