<?php
session_start();
if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

if (!isset($_GET['roll_no'])) {
    die("Invalid student");
}

$roll_no = $_GET['roll_no'];

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

/* ===== PRACTICE BEHAVIOUR ===== */
$practice = $conn->query("
    SELECT 
        COUNT(*) AS attempts,
        MAX(last_attempt_at) AS last_practice,
        COUNT(DISTINCT DATE(last_attempt_at)) AS active_days
    FROM practice_attempts
    WHERE roll_no = '$roll_no'
")->fetch_assoc();

/* ===== SAFE DEFAULTS ===== */
$attempts = (int)($practice['attempts'] ?? 0);
$active_days = (int)($practice['active_days'] ?? 0);

if ($practice['last_practice']) {
    $days_since_last = (new DateTime())->diff(new DateTime($practice['last_practice']))->days;
} else {
    $days_since_last = 999; // never practiced
}

/* ===== LEI CALCULATION ===== */
$effort_score = min(40, $attempts * 2);
$consistency_score = min(40, $active_days * 3);
$recency_score = max(0, 20 - $days_since_last);

$lei = $effort_score + $consistency_score + $recency_score;
$lei = min(100, $lei);

/* ===== LEI BAND ===== */
if ($lei >= 70) {
    $lei_status = "Stable Learner";
    $lei_color = "green";
} elseif ($lei >= 40) {
    $lei_status = "Needs Reinforcement";
    $lei_color = "yellow";
} else {
    $lei_status = "Immediate Support";
    $lei_color = "red";
}

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Student Profile | 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;
}

.back a{
color:#60a5fa;
text-decoration:none;
font-size:14px;
}

.card{
background:#020617;
padding:30px;
border-radius:22px;
box-shadow:0 0 30px rgba(0,0,0,.7);
max-width:900px;
}

h1{margin-bottom:5px}
.subtitle{color:#9ca3af;margin-bottom:25px}

.grid{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(260px,1fr));
gap:25px;
}

.stat{
background:#0f172a;
padding:18px;
border-radius:16px;
}
.stat h3{margin:0 0 8px;font-size:15px;color:#cbd5f5}
.stat p{margin:0;font-size:22px;font-weight:bold}

.lei-card{
text-align:center;
padding:30px;
border-radius:20px;
background:#020617;
box-shadow:0 0 25px rgba(0,0,0,.8);
}

.lei-score{
font-size:56px;
font-weight:bold;
margin:10px 0;
}

.green{color:#22c55e}
.yellow{color:#eab308}
.red{color:#ef4444}

.lei-label{
font-size:14px;
color:#9ca3af;
}

.timeline{
margin-top:30px;
}
</style>
</head>

<body>

<div class="back">
  <a href="teacher_students.php">← Back to Students</a>
</div>

<div class="card">

<h1>Student Learning Passport</h1>
<div class="subtitle">Roll No: <?= htmlspecialchars($roll_no) ?></div>

<div class="grid">

  <!-- LEI CARD -->
  <div class="lei-card">
      <div class="lei-label">Learning Effectiveness Index</div>
      <div class="lei-score <?= $lei_color ?>">
          <?= $lei ?>
      </div>
      <div class="lei-label"><?= $lei_status ?></div>
  </div>

  <!-- PRACTICE STATS -->
  <div class="stat">
    <h3>Total Practice Attempts</h3>
    <p><?= $attempts ?></p>
  </div>

  <div class="stat">
    <h3>Active Days (last period)</h3>
    <p><?= $active_days ?></p>
  </div>

  <div class="stat">
    <h3>Days Since Last Practice</h3>
    <p><?= $days_since_last < 999 ? $days_since_last : "Never" ?></p>
  </div>

</div>

</div>

</body>
</html>
