<?php
// dashboard_learning_passport.php
// RS Learning Lab – Student Dashboard with Momentum Card

/* ===============================
   DB CONFIG
================================ */
$db_host = "localhost";
$db_name = "rs_lab";
$db_user = "root";
$db_pass = "";

try {
    $pdo = new PDO(
        "mysql:host=$db_host;dbname=$db_name;charset=utf8mb4",
        $db_user,
        $db_pass,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
} catch (PDOException $e) {
    die("DB connection failed");
}

/* ===============================
   INPUT
================================ */
$roll_no = $_GET['roll'] ?? null;

if (!$roll_no) {
    die("Student not specified");
}

/* ===============================
   FETCH LEARNING PROFILE
================================ */
$stmt = $pdo->prepare("
    SELECT *
    FROM learning_profiles
    WHERE roll_no = :roll
    LIMIT 1
");
$stmt->execute([':roll' => $roll_no]);
$profile = $stmt->fetch();

if (!$profile) {
    die("Learning profile not found");
}

$style_scores = json_decode($profile['style_scores'], true) ?? [];
$learning_signals = json_decode($profile['learning_signals'], true) ?? [];

/* ===============================
   FETCH MOMENTUM
================================ */
$stmt = $pdo->prepare("
    SELECT momentum_score, momentum_state, updated_at
    FROM learning_momentum
    WHERE roll_no = :roll
    LIMIT 1
");
$stmt->execute([':roll' => $roll_no]);
$momentum = $stmt->fetch();

$momentum_score = $momentum['momentum_score'] ?? 50;
$momentum_state = $momentum['momentum_state'] ?? "Building";
$momentum_updated = $momentum['updated_at'] ?? null;

/* ===============================
   MOMENTUM UI MAPPING
================================ */
$momentum_color = [
    "Applying" => "#22c55e",
    "Stabilizing" => "#38bdf8",
    "Building" => "#facc15",
    "Reset" => "#ef4444"
];

$momentum_text = [
    "Applying" => "Applying concepts confidently",
    "Stabilizing" => "Understanding is settling steadily",
    "Building" => "Learning habit is forming",
    "Reset" => "Needs calm practice reinforcement"
];

$bar_color = $momentum_color[$momentum_state] ?? "#38bdf8";
?>

<!DOCTYPE html>
<html>
<head>
<title>Student Dashboard – RS Learning Lab</title>
<style>
body {
    background:#0b1020;
    color:#e5e7eb;
    font-family:Arial, sans-serif;
    padding:30px;
}
.container {
    max-width:1100px;
    margin:auto;
}
h1 {
    color:#38bdf8;
}
.grid {
    display:grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap:20px;
}
.card {
    background:#111827;
    padding:22px;
    border-radius:14px;
}
.label {
    color:#93c5fd;
    font-size:14px;
}
.value {
    font-size:16px;
    margin-bottom:8px;
}
.badge {
    display:inline-block;
    padding:6px 14px;
    border-radius:20px;
    background:#0b8c9f;
    color:#fff;
    font-size:14px;
}
.badge.secondary {
    background:#334155;
}
.badge.lei {
    background:#022c22;
    color:#6ee7b7;
}
.progress {
    height:10px;
    background:#020617;
    border-radius:8px;
    overflow:hidden;
    margin-top:10px;
}
.progress-fill {
    height:100%;
}
ul {
    padding-left:20px;
}
.footer-note {
    margin-top:30px;
    font-size:13px;
    color:#94a3b8;
}
.btn {
    display:inline-block;
    background:#0ea5e9;
    padding:10px 16px;
    border-radius:8px;
    color:#fff;
    text-decoration:none;
    font-size:14px;
    margin-top:15px;
}
</style>
</head>

<body>

<div class="container">

<h1>Student Learning Dashboard</h1>

<div class="grid">

<!-- STUDENT INFO -->
<div class="card">
    <p class="label">Student Name</p>
    <p class="value"><?php echo htmlspecialchars($profile['student_name']); ?></p>

    <p class="label">Roll No</p>
    <p class="value"><?php echo htmlspecialchars($profile['roll_no']); ?></p>

    <p class="label">Class</p>
    <p class="value"><?php echo htmlspecialchars($profile['class']); ?></p>
</div>

<!-- LEARNING STYLE -->
<div class="card">
    <h3>Learning Style</h3>
    <span class="badge"><?php echo $profile['primary_style']; ?></span>
    <span class="badge secondary"><?php echo $profile['secondary_style']; ?></span>
</div>

<!-- LEI -->
<div class="card">
    <h3>Learning Effectiveness Index</h3>
    <span class="badge lei"><?php echo $profile['lei_signal']; ?></span>
    <p class="footer-note">
        LEI reflects effort, improvement, and consistency.  
        It is not a score or rank.
    </p>
</div>

<!-- 🚀 MOMENTUM CARD (NEW) -->
<div class="card">
    <h3>Learning Momentum</h3>

    <p>
        <strong><?php echo $momentum_state; ?></strong><br>
        <span class="label"><?php echo $momentum_text[$momentum_state]; ?></span>
    </p>

    <div class="progress">
        <div class="progress-fill"
             style="width: <?php echo $momentum_score; ?>%;
                    background: <?php echo $bar_color; ?>;">
        </div>
    </div>

    <p class="footer-note">
        Momentum shows persistence and recovery during learning.
        <?php if ($momentum_updated): ?>
            <br>Last updated: <?php echo date("d M Y, h:i A", strtotime($momentum_updated)); ?>
        <?php endif; ?>
    </p>

    <a class="btn"
       href="momentum_timeline.php?roll=<?php echo urlencode($roll_no); ?>&name=<?php echo urlencode($profile['student_name']); ?>">
       View Momentum Timeline
    </a>
</div>

</div>

<!-- LEARNING SIGNALS -->
<div class="card" style="margin-top:20px;">
    <h3>Learning Signals</h3>
    <ul>
        <?php foreach ($learning_signals as $signal): ?>
            <li><?php echo htmlspecialchars($signal); ?></li>
        <?php endforeach; ?>
    </ul>
</div>

<!-- STYLE PATTERN -->
<div class="card" style="margin-top:20px;">
    <h3>Learning Pattern Overview</h3>
    <div class="grid">
        <?php foreach ($style_scores as $style => $score): ?>
            <div class="card">
                <strong><?php echo $style; ?></strong><br>
                <span><?php echo $score; ?> signal units</span>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- PASSPORT -->
<div class="card" style="margin-top:20px;">
    <h3>Learning Passport</h3>
    <p class="footer-note">
        This passport evolves as the student practices, reinforces,
        and applies learning over time.
    </p>

    <a class="btn"
       href="learning_passport.php?
       name=<?php echo urlencode($profile['student_name']); ?>&
       roll=<?php echo urlencode($profile['roll_no']); ?>&
       class=<?php echo urlencode($profile['class']); ?>&
       primary=<?php echo urlencode($profile['primary_style']); ?>&
       secondary=<?php echo urlencode($profile['secondary_style']); ?>&
       lei=<?php echo urlencode($profile['lei_signal']); ?>&
       signals=<?php echo urlencode(implode(',', $learning_signals)); ?>">
       View Learning Passport PDF
    </a>
</div>

<div class="footer-note">
© 2026 RS Learning Lab · Learning · Momentum · Growth
</div>

</div>

</body>
</html>
