<?php
session_start();
require_once(__DIR__ . "/../config/db.php");

/* 🔒 Teacher check */
if (!isset($_SESSION['teacher_id'])) {
    die("Unauthorized");
}

/* GET ALL PROJECTS */
$stmt = $pdo->prepare("
    SELECT ps.*, s.student_name 
    FROM project_submissions ps
    JOIN students s ON ps.student_id = s.id
    ORDER BY ps.submitted_at DESC
");
$stmt->execute();
$projects = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
<title>Project Reviews</title>
<style>
body{background:#020617;color:white;font-family:Arial;}
.container{width:90%;margin:30px auto;}
.card{background:#0f172a;padding:20px;margin-bottom:20px;border-radius:12px;}
textarea{width:100%;height:100px;margin-top:10px;}
input{padding:6px;margin-top:10px;}
button{margin-top:10px;padding:10px;background:#22c55e;border:none;}
</style>
</head>

<body>

<div class="container">

<h2>Student Projects 🧠💻</h2>

<?php foreach($projects as $p): ?>

<div class="card">

<h3><?= htmlspecialchars($p['student_name']) ?></h3>

<p><b>Submitted At:</b> <?= $p['submitted_at'] ?></p>

<pre style="background:#020617;padding:10px;">
<?= htmlspecialchars($p['code']) ?>
</pre>

<form method="POST" action="save_review.php">

<input type="hidden" name="id" value="<?= $p['id'] ?>">

<label>Score:</label><br>
<input type="number" name="score" value="<?= $p['score'] ?>"><br>

<label>Feedback:</label><br>
<textarea name="feedback"><?= $p['feedback'] ?></textarea>

<br>

<button>Save Review</button>

</form>

</div>

<?php endforeach; ?>

</div>

</body>
</html>