<?php
require_once(__DIR__ . "/../header.php");
require_once(__DIR__ . "/../../public/connection.php");

// =============================
// STUDENT AUTH CHECK
// =============================
if (!isset($_SESSION['student']) || empty($_SESSION['student'])) {
    die("<div class='alert alert-danger m-4'>Access denied. Please log in as a student.</div>");
}

$student_id = $_SESSION['student'];

// =============================
// FETCH STUDENT CLASS
// =============================
$st_q = $connection->prepare("SELECT class, firstname, lastname FROM students WHERE id=?");
$st_q->bind_param("i", $student_id);
$st_q->execute();
$student = $st_q->get_result()->fetch_assoc();
$class = $student['class'] ?? null;

if (!$class) {
    die("<div class='alert alert-danger m-4'>Student record not found or class not assigned.</div>");
}

// =============================
// FETCH SUBMITTED ASSIGNMENTS
// =============================
$stmt = $connection->prepare("
    SELECT a.id AS assignment_id, a.title, a.subject, a.description, a.due_date, a.file_path AS assignment_file,
           s.id AS submission_id, s.file_path AS submission_file, s.submission_time, s.grade, s.comment
    FROM assignments a
    JOIN submissions s ON a.id = s.assignment_id
    WHERE s.student_id = ?
    ORDER BY s.submission_time DESC
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$submissions = $stmt->get_result();
?>

<link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../../assets/css/style.css">
<link rel="stylesheet" href="../../assets/css/custom.css">

<style>
.card-modern {
    border-radius: 0.75rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    transition: transform 0.2s ease-in-out;
    overflow: hidden;
}
.card-modern:hover {
    transform: translateY(-3px);
}
.assignment-banner {
    background-color: #28a745;
    color: white;
    padding: 1rem 1.25rem;
    font-size: 1.1rem;
    font-weight: bold;
    text-align: center;
}
</style>

<div class="container mt-4">
    <h3 class="mb-4"><i class="fa fa-file-alt me-2 text-success"></i>Submitted Assignments</h3>

    <?php if ($submissions->num_rows === 0): ?>
        <div class="alert alert-info">You have not submitted any assignments yet.</div>
    <?php else: ?>
        <?php while ($sub = $submissions->fetch_assoc()): ?>
            <div class="card card-modern shadow-sm mb-3">
                <?php if (!empty($sub['grade'])): ?>
                    <div class="assignment-banner">
                        This Assignment Has Been Graded ✅
                    </div>
                <?php endif; ?>

                <div class="card-header <?= empty($sub['grade']) ? 'bg-primary text-white' : 'bg-light text-dark' ?>">
                    <h5 class="mb-0"><?= htmlspecialchars($sub['title']) ?> - <?= htmlspecialchars($sub['subject']) ?></h5>
                </div>
                <div class="card-body">
                    <p><strong>Description:</strong> <?= nl2br(htmlspecialchars($sub['description'])) ?></p>
                    <p><strong>Due Date:</strong> <?= date("d M Y, h:i A", strtotime($sub['due_date'])) ?></p>

                    <?php if (!empty($sub['assignment_file'])): ?>
                        <p>
                            <strong>Assignment File:</strong> 
                            <a href="<?= $sub['assignment_file'] ?>" target="_blank" class="btn btn-outline-primary btn-sm">Download</a>
                        </p>
                    <?php endif; ?>

                    <p>
                        <strong>Your Submission:</strong> 
                        <?php if (!empty($sub['submission_file'])): ?>
                            <a href="<?= $sub['submission_file'] ?>" target="_blank" class="btn btn-outline-success btn-sm">View/Download</a>
                        <?php else: ?>
                            Not uploaded
                        <?php endif; ?>
                    </p>

                    <p>
                        <strong>Submitted At:</strong> <?= date("d M Y, h:i A", strtotime($sub['submission_time'])) ?>
                    </p>

                    <p>
                        <strong>Grade:</strong> <?= htmlspecialchars($sub['grade'] ?? 'Not graded yet') ?>
                    </p>

                    <p>
                        <strong>Teacher Comment:</strong> <?= htmlspecialchars($sub['comment'] ?? '-') ?>
                    </p>
                </div>
            </div>
        <?php endwhile; ?>
    <?php endif; ?>
</div>
