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

$sql = "
SELECT 
    pa.roll_no,
    MAX(pa.last_attempt_at) AS last_practice_date
FROM practice_attempts pa
GROUP BY pa.roll_no
HAVING 
    MAX(pa.last_attempt_at) < NOW() - INTERVAL 10 DAY
ORDER BY last_practice_date ASC
LIMIT 25
";

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

<!DOCTYPE html>
<html>
<head>
    <title>Stalled Learners</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: #0f172a;
            color: #e5e7eb;
        }
        h2 {
            text-align: center;
            margin-top: 40px;
            color: #ef4444;
        }
        table {
            width: 55%;
            margin: 30px auto;
            border-collapse: collapse;
            background: #020617;
            box-shadow: 0 0 15px rgba(0,0,0,0.6);
            border-radius: 10px;
            overflow: hidden;
        }
        th, td {
            padding: 14px;
            border-bottom: 1px solid #1e293b;
            text-align: left;
        }
        th {
            background: #7f1d1d;
            color: #fecaca;
        }
        tr:hover {
            background: #020617;
        }
        td {
            color: #e5e7eb;
        }
        .empty {
            text-align: center;
            padding: 20px;
            color: #9ca3af;
        }
    </style>
</head>
<body>

<h2>🚨 Stalled Learners (10+ Days No Practice)</h2>

<table>
    <tr>
        <th>Roll No</th>
        <th>Last Practice Date</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>
                  </tr>";
        }
    } else {
        echo "<tr><td colspan='2' class='empty'>No stalled learners 🎉</td></tr>";
    }
    ?>

</table>

</body>
</html>

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