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

// Ensure teacher is logged in
if (!isset($_SESSION['staff'])) {
    header("Location: ../../login.php");
    exit;
}

$teacher_id = $_SESSION['staff'];

// Fetch all assignments by this teacher
$stmt = $connection->prepare("SELECT * FROM assignments WHERE teacher_id=? ORDER BY due_date DESC");
$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$assignments_res = $stmt->get_result();
$assignments = [];
while ($row = $assignments_res->fetch_assoc()) {
    $assignments[] = $row;
}

// Fetch only graded submissions per assignment
$all_submissions = [];
foreach ($assignments as $assignment) {
    $stmt = $connection->prepare("
        SELECT s.id AS sub_id, s.file_path, s.grade, s.comment, s.submission_time,
               st.reg_number, st.firstname, st.lastname, st.othername
        FROM submissions s
        JOIN students st ON s.student_id = st.id
        WHERE s.assignment_id=? 
          AND s.grade IS NOT NULL 
          AND s.grade != ''
        ORDER BY s.submission_time DESC
    ");
    $stmt->bind_param("i", $assignment['id']);
    $stmt->execute();
    $res = $stmt->get_result();

    $subs = [];
    while ($sub = $res->fetch_assoc()) {
        $subs[] = $sub;
    }

    if (!empty($subs)) {
        $all_submissions[$assignment['id']] = $subs;
    }
}
?>

<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">
<script src="../../assets/js/jquery.min.js"></script>
<script src="../../assets/js/bootstrap.bundle.min.js"></script>

<div class="container mt-4">
    <h3 class="mb-3">Graded Assignments</h3>

    <?php if (empty($all_submissions)): ?>
        <div class="alert alert-info">
            No graded submissions available.
        </div>
    <?php else: ?>

        <?php foreach ($assignments as $assignment): ?>
            <?php if (!isset($all_submissions[$assignment['id']])) continue; ?>

            <div class="card shadow-sm mb-4">
                <div class="card-header bg-success text-white">
                    <strong>
                        <?= htmlspecialchars($assignment['title']) ?>
                        — <?= htmlspecialchars($assignment['subject']) ?>
                        (<?= htmlspecialchars($assignment['class']) ?>)
                    </strong>
                </div>

                <div class="card-body p-0">

                    <!-- Assignment details (DESKTOP ONLY) -->
                    <div class="p-3 d-none d-md-block">
                        <p><strong>Description:</strong><br>
                            <?= nl2br(htmlspecialchars($assignment['description'])) ?>
                        </p>

                        <p>
                            <strong>Due Date:</strong>
                            <?= date("d M, Y h:i A", strtotime($assignment['due_date'])) ?>
                        </p>

                        <?php if (!empty($assignment['file_path'])): ?>
                            <p>
                                <strong>Attachment:</strong>
                                <a href="<?= $assignment['file_path'] ?>" target="_blank">Download</a>
                            </p>
                        <?php endif; ?>
                    </div>

                    <!-- Table (PRIMARY VIEW FOR MOBILE & DESKTOP) -->
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Student</th>
                                    <th>Submitted</th>
                                    <th>File</th>
                                    <th>Grade</th>
                                    <th>Comment</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($all_submissions[$assignment['id']] as $s): ?>
                                    <tr>
                                        <td>
                                            <?= htmlspecialchars(
                                                $s['firstname'] . ' ' .
                                                $s['lastname'] . ' ' .
                                                $s['othername']
                                            ) ?>
                                            <br>
                                            <small class="text-muted">
                                                <?= htmlspecialchars($s['reg_number']) ?>
                                            </small>
                                        </td>

                                        <td>
                                            <?= date("d M, Y", strtotime($s['submission_time'])) ?><br>
                                            <small class="text-muted">
                                                <?= date("h:i A", strtotime($s['submission_time'])) ?>
                                            </small>
                                        </td>

                                        <td>
                                            <?php if ($s['file_path']): ?>
                                                <a href="<?= $s['file_path'] ?>" target="_blank">
                                                    View
                                                </a>
                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </td>

                                        <td>
                                            <span class="badge bg-success">
                                                <?= htmlspecialchars($s['grade']) ?>
                                            </span>
                                        </td>

                                        <td>
                                            <?= htmlspecialchars($s['comment'] ?: '-') ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>

        <?php endforeach; ?>

    <?php endif; ?>
</div>
