<?php
session_start();

/* DB (keep safe) */
require_once(__DIR__ . "/config/db.php");

if (!isset($_SESSION['final_attempted'])) {
    die("Unauthorized access");
}

$name  = $_SESSION['student_name'] ?? 'Student';
$roll  = $_SESSION['student_id'] ?? '';
$class = $_SESSION['student_class'] ?? ($_GET['class'] ?? '');
$class = trim($class);
$score = $_SESSION['final_score'] ?? 0;

$passed = $score >= 60;

/* Badge trigger */
if ($passed) {
    $_SESSION['award_badge'] = true;
}

/* 🔥 IMPORTANT FIX */
$topic_id = $_GET['topic_id'] ?? 1;

/* 🔥 STRONG CLASS CHECK */
$isEligible = true;

?>
<!DOCTYPE html>
<html>
<head>
<title>Final Assessment Result | RS Learning Lab</title>
<style>
body{
margin:0;
min-height:100vh;
background:radial-gradient(circle at top,#020617,#0f172a);
font-family:Arial;
color:#e5e7eb;
display:flex;
align-items:center;
justify-content:center;
}
.card{
background:#020617;
padding:50px 60px;
border-radius:22px;
box-shadow:0 0 45px rgba(0,0,0,.8);
max-width:560px;
text-align:center;
}
h1{margin-bottom:12px}
.score{
font-size:26px;
margin-bottom:14px;
}
.pass{color:#22c55e}
.fail{color:#f87171}
.cta{
display:inline-block;
margin-top:22px;
padding:14px 34px;
border-radius:999px;
font-weight:bold;
text-decoration:none;
}
.primary{
background:linear-gradient(135deg,#22c55e,#06b6d4);
color:#020617;
}
.secondary{
background:#1f2937;
color:#e5e7eb;
}
.footer{
margin-top:30px;
font-size:13px;
opacity:.55;
}
</style>
</head>
<body>

<div class="card">
<h1>🏁 Final Assessment Result</h1>

<div class="score <?= $passed ? 'pass' : 'fail' ?>">
Your Score: <?= round($score) ?>%
</div>

<?php if ($passed): ?>
<p>🎉 Excellent work, <b><?= htmlspecialchars($name) ?></b>!  
You have successfully completed the final assessment.</p>

<a href="/rs_lab/certificates/generate_certificate_pdf.php
?name=<?= urlencode($_SESSION['student_name']) ?>
&roll=<?= urlencode($_SESSION['student_id']) ?>
&class=<?= urlencode($_SESSION['student_class']) ?>"
class="cta primary">
🎓 Download Certificate
</a>

<?php if ($isEligible): ?>
<br><br>

<a href="/rs_lab/coding/mini_project.php?topic_id=<?= $topic_id ?>"
class="cta primary">
🚀 Start Mini Project
</a>

<?php endif; ?>

<?php else: ?>
<p>💪 Keep going! You are improving.  
Revise the concepts and try again.</p>

<a class="cta secondary"
href="/rs_lab/final_track.php
?name=<?= urlencode($name) ?>
&roll=<?= urlencode($roll) ?>
&class=<?= urlencode($class) ?>">
🔁 Retry Final Assessment
</a>
<?php endif; ?>

<br>

<a class="cta secondary"
href="/rs_lab/student/lei_dashboard.php">
⬅ Back to Dashboard
</a>

<div class="footer">
© RS Learning Lab — Learning Behaviour Platform
</div>
</div>

</body>
</html>