<?php
session_start();

/* 🔐 ONLY TEACHER ALLOWED */
if (!isset($_SESSION['teacher_id']) || ($_SESSION['user_role'] ?? '') !== 'teacher') {
    header("Location: /rs_lab/teacher_login.php");
    exit();
}

$conn = new mysqli("localhost", "root", "", "rs_lab");
if ($conn->connect_error) die("DB Connection failed");

// Active learners (last 7 days)
$active = $conn->query("
    SELECT COUNT(DISTINCT roll_no) AS cnt
    FROM practice_attempts
    WHERE last_attempt_at >= NOW() - INTERVAL 7 DAY
")->fetch_assoc()['cnt'] ?? 0;

// Slowing learners (8–10 days)
$slowing = $conn->query("
    SELECT COUNT(*) AS cnt FROM (
        SELECT roll_no, MAX(last_attempt_at) last_practice
        FROM practice_attempts
        GROUP BY roll_no
        HAVING last_practice BETWEEN 
            NOW() - INTERVAL 10 DAY AND NOW() - INTERVAL 8 DAY
    ) t
")->fetch_assoc()['cnt'] ?? 0;

// Stalled learners (10+ days)
$stalled = $conn->query("
    SELECT COUNT(*) AS cnt FROM (
        SELECT roll_no, MAX(last_attempt_at) last_practice
        FROM practice_attempts
        GROUP BY roll_no
        HAVING last_practice < NOW() - INTERVAL 10 DAY
    ) t
")->fetch_assoc()['cnt'] ?? 0;

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Teacher Dashboard | RS Learning Lab</title>
<style>
body{
margin:0;
font-family:Arial, sans-serif;
background:radial-gradient(circle at top,#020617,#0f172a);
color:#e5e7eb;
padding:40px;
}

h1{margin-bottom:8px}
.subtitle{color:#9ca3af;margin-bottom:30px}

.cards{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(240px,1fr));
gap:25px;
max-width:900px;
}

.card{
background:#020617;
padding:30px;
border-radius:22px;
box-shadow:0 0 25px rgba(0,0,0,.7);
cursor:pointer;
transition:all .3s ease;
}

.card:hover{
transform:translateY(-6px);
box-shadow:0 0 35px rgba(255,255,255,.08);
}

.card h2{margin:0;font-size:20px}
.card p{margin:10px 0 0;color:#9ca3af}

.count{
font-size:42px;
font-weight:bold;
margin-top:15px;
}

.green{border:1px solid rgba(34,197,94,.4)}
.green .count{color:#22c55e}

.yellow{border:1px solid rgba(234,179,8,.4)}
.yellow .count{color:#eab308}

.red{border:1px solid rgba(239,68,68,.4)}
.red .count{color:#ef4444}
</style>
</head>

<body>

<h1>Teacher Dashboard</h1>
<div class="subtitle">
Monitor learning activity, momentum, and student engagement.
</div>

<div class="cards">

    <div class="card green" onclick="location.href='active_learners.php'">
        <h2>🟢 Active Learners</h2>
        <p>Practiced in last 7 days</p>
        <div class="count"><?= $active ?></div>
    </div>

    <div class="card yellow" onclick="location.href='slowing_learners.php'">
        <h2>🟡 Slowing Learners</h2>
        <p>Practice gap forming</p>
        <div class="count"><?= $slowing ?></div>
    </div>

    <div class="card red" onclick="location.href='stalled_students.php'">
        <h2>🔴 Stalled Learners</h2>
        <p>10+ days no practice</p>
        <div class="count"><?= $stalled ?></div>
    </div>

</div>

</body>
</html>
