<?php
session_start();
include 'db_connection.php';

/* ================= AUTH ================= */
if (
    !isset($_SESSION['student_id']) ||
    $_SESSION['role'] !== 'student'
) {
    header("Location: student_login.php?error=Unauthorized");
    exit();
}

$student_id = $_SESSION['student_id'];

/* ================= FETCH DATA ================= */
$sql = "SELECT h.title, h.description, sh.submitted_at, sh.file_path, sh.score, sh.feedback
        FROM student_homework sh
        JOIN homework h ON sh.homework_id = h.homework_id
        WHERE sh.student_id = ?
        ORDER BY sh.submitted_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Feedback & Grading</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body {
    background: linear-gradient(135deg, #eef2f7, #d9e4f5);
    font-family: 'Segoe UI', sans-serif;
}

/* HEADER */
.header {
    background: linear-gradient(to right, #4facfe, #00f2fe);
    color: white;
    padding: 20px;
    text-align: center;
    border-radius: 0 0 15px 15px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

/* CARD */
.card-custom {
    border: none;
    border-radius: 15px;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.card-custom:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

/* BADGES */
.badge-score {
    font-size: 14px;
    padding: 6px 10px;
}

.badge-success { background: #28a745; }
.badge-warning { background: #ffc107; color: black; }
.badge-secondary { background: #6c757d; }

/* LINK */
.file-link {
    text-decoration: none;
    font-weight: 500;
}

/* EMPTY STATE */
.empty-box {
    text-align: center;
    padding: 50px;
    color: #777;
}
</style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h3><i class="fas fa-comments"></i> Feedback & Grading</h3>
    <p>Review your homework submissions and teacher feedback</p>
</div>

<div class="container mt-4">

<?php if ($result->num_rows > 0): ?>
    <div class="row g-4">

        <?php while ($row = $result->fetch_assoc()): ?>
        <div class="col-md-6">
            <div class="card card-custom shadow p-3">

                <h5><i class="fas fa-book text-primary"></i> 
                    <?php echo htmlspecialchars($row['title']); ?>
                </h5>

                <p class="text-muted">
                    <?php echo htmlspecialchars($row['description']); ?>
                </p>

                <hr>

                <p>
                    <i class="fas fa-calendar-alt"></i>
                    <strong>Submitted:</strong>
                    <?php echo date("d M Y, h:i A", strtotime($row['submitted_at'])); ?>
                </p>

                <p>
                    <i class="fas fa-file"></i>
                    <strong>File:</strong>
                    <a class="file-link text-primary"
                       href="/csms/student/uploads/<?php echo htmlspecialchars(basename($row['file_path'])); ?>"
                       target="_blank">
                       View Submission
                    </a>
                </p>

                <hr>

                <!-- SCORE -->
                <p>
                    <strong>Score:</strong>
                    <?php if (!empty($row['score'])): ?>
                        <span class="badge badge-success badge-score">
                            <?php echo $row['score']; ?>
                        </span>
                    <?php else: ?>
                        <span class="badge badge-secondary badge-score">
                            Not graded
                        </span>
                    <?php endif; ?>
                </p>

                <!-- FEEDBACK -->
                <p>
                    <strong>Feedback:</strong><br>
                    <?php echo !empty($row['feedback']) 
                        ? htmlspecialchars($row['feedback']) 
                        : "<span class='text-muted'>No feedback yet</span>"; ?>
                </p>

            </div>
        </div>
        <?php endwhile; ?>

    </div>

<?php else: ?>
    <div class="empty-box">
        <i class="fas fa-folder-open fa-3x mb-3"></i>
        <h5>No Feedback Yet</h5>
        <p>Your submitted homework feedback will appear here.</p>
    </div>
<?php endif; ?>

</div>

</body>
</html>

<?php
$stmt->close();
$conn->close();
?>