<?php
// DB Connection
$conn = new mysqli("localhost", "root", "", "rs_lab");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

/*
 SLOWING LEARNERS:
 - Last practice between 3 and 5 days ago
*/

$sql = "
SELECT 
    pa.roll_no,
    MAX(pa.last_attempt_at) AS last_practice_date,
    DATEDIFF(NOW(), MAX(pa.last_attempt_at)) AS days_gap
FROM practice_attempts pa
GROUP BY pa.roll_no
HAVING 
    days_gap BETWEEN 3 AND 5
ORDER BY days_gap DESC
";

$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Slowing Learners</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: #0f172a;
            color: #e5e7eb;
        }
        h2 {
            text-align: center;
            margin-top: 40px;
            color: #facc15;
        }
        .context {
            text-align: center;
            color: #9ca3af;
            margin-top: 10px;
        }
        table {
            width: 60%;
            margin: 30px auto;
            border-collapse: collapse;
            background: #020617;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 0 15px rgba(0,0,0,0.6);
        }
        th, td {
            padding: 14px;
            border-bottom: 1px solid #1e293b;
            text-align: left;
        }
        th {
            background: #713f12;
            color: #fde68a;
        }
        td {
            color: #e5e7eb;
        }
        .gap-warning {
            color: #facc15;
            font-weight: bold;
        }
        .empty {
            text-align: center;
            padding: 20px;
            color: #9ca3af;
        }
    </style>
</head>
<body>

<h2>🟡 Slowing Learners (3–5 Days Gap)</h2>
<div class="context">
    These students are active but learning momentum is slowing.
</div>

<table>
    <tr>
        <th>Roll No</th>
        <th>Last Practice Date</th>
        <th>Days Since Last Practice</th>
    </tr>

    <?php
    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo "<tr>
                    <td>{$row['roll_no']}</td>
                    <td>{$row['last_practice_date']}</td>
                    <td class='gap-warning'>{$row['days_gap']} day(s)</td>
                  </tr>";
        }
    } else {
        echo "<tr>
                <td colspan='3' class='empty'>No slowing learners 🎉</td>
              </tr>";
    }
    ?>

</table>

</body>
</html>

<?php $conn->close(); ?>
