<?php
session_start();
require_once('../config.php');

// ✅ Restrict access to students only
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'student') {
    header("Location: login.php");
    exit();
}

include('../header/header.php');

// ✅ Use ONE consistent variable
$student_id = $_SESSION['user_id'];

// ✅ Fetch results (FIXED)
$stmt = $pdo->prepare("
    SELECT r.*, c.course_name 
    FROM results r 
    JOIN courses c ON r.course_id = c.id 
    WHERE r.student_id = ? 
    ORDER BY r.created_at DESC
");
$stmt->execute([$student_id]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// ✅ Calculate performance
$total_score = 0;
$total_courses = count($results);

foreach ($results as $row) {
    $total_score += $row['score'];
}

$average_score = ($total_courses > 0) ? ($total_score / $total_courses) : 0;
?>

<div class="container-fluid py-4">

    <!-- Summary Cards -->
    <div class="row g-4 mb-5">
        
        <div class="col-md-6 col-lg-4">
            <div class="card shadow-sm border-0 bg-primary text-white p-4">
                <h5 class="fw-bold mb-2">Academic Performance</h5>
                <h2 class="fw-bold"><?php echo number_format($average_score, 1); ?>%</h2>
                <small>Average Score</small>
            </div>
        </div>

        <div class="col-md-6 col-lg-4">
            <div class="card shadow-sm border-0 bg-success text-white p-4">
                <h5 class="fw-bold mb-2">Total Courses</h5>
                <h2 class="fw-bold"><?php echo $total_courses; ?></h2>
                <small>Completed Courses</small>
            </div>
        </div>

    </div>

    <!-- Results Table -->
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white">
            <h4 class="fw-bold mb-0">Course Results & Performance</h4>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">

                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Course Name</th>
                            <th>Score</th>
                            <th>Grade</th>
                            <th>Remarks</th>
                            <th class="text-end pe-4">Date Released</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php if (empty($results)): ?>
                            <tr>
                                <td colspan="5" class="text-center py-4 text-muted">
                                    No results have been released yet.
                                </td>
                            </tr>
                        <?php else: ?>

                            <?php foreach($results as $result): 
                                $score = (float)$result['score'];
                            ?>

                            <tr>
                                <!-- Course -->
                                <td class="ps-4 fw-bold">
                                    <?php echo htmlspecialchars($result['course_name']); ?>
                                </td>

                                <!-- Score + Progress -->
                                <td>
                                    <div class="fw-bold"><?php echo $score; ?>%</div>
                                    <div class="progress" style="height:6px; width:100px;">
                                        <div class="progress-bar bg-primary" 
                                             style="width: <?php echo $score; ?>%">
                                        </div>
                                    </div>
                                </td>

                                <!-- Grade -->
                                <td>
                                    <span class="badge bg-primary">
                                        <?php echo htmlspecialchars($result['grade']); ?>
                                    </span>
                                </td>

                                <!-- Remarks -->
                                <td class="text-muted">
                                    <?php echo htmlspecialchars($result['remarks']); ?>
                                </td>

                                <!-- Date -->
                                <td class="text-end pe-4 small text-muted">
                                    <?php echo date('M d, Y', strtotime($result['created_at'])); ?>
                                </td>
                            </tr>

                            <?php endforeach; ?>

                        <?php endif; ?>
                    </tbody>

                </table>

            </div>
        </div>
    </div>

</div>

<?php include('../footer/footer.php'); ?>