<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Homework Page
|--------------------------------------------------------------------------
*/

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/student-auth.php';

$pdo = db();

requireStudent();

$student = currentStudent($pdo);
$courseId = (int)($_GET['course_id'] ?? 0);

if ($courseId <= 0) {
    redirectTo('student/my-courses.php');
}

if (!$student) {

    studentLogout();

    redirectTo('login.php');
}
$stmt = $pdo->prepare("
    SELECT c.*
    FROM core_courses c
    INNER JOIN core_course_students cs
        ON cs.course_id = c.id
    WHERE c.id = ?
      AND cs.student_id = ?
      AND cs.status = 'active'
    LIMIT 1
");

$stmt->execute([
    $courseId,
    (int)$student['id']
]);

$course = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$course) {
    redirectTo('student/my-courses.php');
}
/*
|--------------------------------------------------------------------------
| Homework List
|--------------------------------------------------------------------------
*/

$homeworks = [];

if (tableExists($pdo, 'core_homework')) {

    $stmt = $pdo->prepare("
    SELECT *
    FROM core_homework
    WHERE course_id = ?
      AND status = 'active'
    ORDER BY due_date DESC
");

   $stmt->execute([$courseId]);
    $homeworks = $stmt->fetchAll();
}

/*
|--------------------------------------------------------------------------
| Student Submissions
|--------------------------------------------------------------------------
*/

$submittedHomework = [];

if (
    tableExists(
        $pdo,
        'core_homework_submissions'
    )
) {

    $submissionStmt = $pdo->prepare("

        SELECT homework_id
        FROM core_homework_submissions

        WHERE student_id = ?

    ");

   $submissionStmt->execute([
    (int)$student['id'],
    $courseId
]);

    $submittedHomework =
        array_column(
            $submissionStmt->fetchAll(),
            'homework_id'
        );
}

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta
name="viewport"
content="width=device-width, initial-scale=1.0">

<title>
Homework
</title>

<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap"
rel="stylesheet">

<style>

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{

    font-family:'Inter',sans-serif;

    background:#f3f6fb;

    color:#0f172a;
}

.page{

    padding:35px;
}

/*
|--------------------------------------------------------------------------
| Topbar
|--------------------------------------------------------------------------
*/

.topbar{

    display:flex;

    justify-content:space-between;

    align-items:center;

    margin-bottom:30px;
}

.page-title{

    font-size:42px;

    font-weight:900;
}

.back-btn{

    text-decoration:none;

    background:#eff6ff;

    color:#2563eb;

    padding:14px 20px;

    border-radius:18px;

    font-weight:800;
}

/*
|--------------------------------------------------------------------------
| Hero
|--------------------------------------------------------------------------
*/

.hero{

    background:
    linear-gradient(135deg,#0f172a,#1d4ed8);

    color:#fff;

    border-radius:34px;

    padding:40px;

    margin-bottom:30px;

    box-shadow:
    0 35px 70px rgba(37,99,235,0.18);
}

.hero h1{

    font-size:44px;

    margin-bottom:14px;

    font-weight:900;
}

.hero p{

    color:#dbeafe;

    line-height:2;
}

/*
|--------------------------------------------------------------------------
| Grid
|--------------------------------------------------------------------------
*/

.grid{

    display:grid;

    grid-template-columns:
    repeat(auto-fit,minmax(350px,1fr));

    gap:24px;
}

/*
|--------------------------------------------------------------------------
| Homework Card
|--------------------------------------------------------------------------
*/

.homework-card{

    background:#fff;

    border-radius:30px;

    overflow:hidden;

    border:1px solid #e2e8f0;

    box-shadow:
    0 15px 40px rgba(15,23,42,0.05);
}

.card-header{

    background:
    linear-gradient(135deg,#2563eb,#1d4ed8);

    color:#fff;

    padding:28px;
}

.card-header h2{

    font-size:28px;

    margin-bottom:12px;

    font-weight:900;
}

.card-header p{

    color:#dbeafe;

    line-height:1.9;
}

.card-body{

    padding:28px;
}

.info-box{

    display:flex;

    justify-content:space-between;

    align-items:center;

    background:#f8fafc;

    border-radius:16px;

    padding:15px 18px;

    margin-bottom:14px;
}

.info-box span{

    color:#64748b;

    font-weight:700;
}

.info-box strong{

    font-weight:800;
}

/*
|--------------------------------------------------------------------------
| Status
|--------------------------------------------------------------------------
*/

.status{

    display:inline-block;

    padding:10px 16px;

    border-radius:999px;

    font-size:12px;
    font-weight:900;
}

.status.submitted{

    background:#dcfce7;

    color:#16a34a;
}

.status.pending{

    background:#fef3c7;

    color:#d97706;
}

/*
|--------------------------------------------------------------------------
| Upload Form
|--------------------------------------------------------------------------
*/

.upload-form{

    margin-top:22px;
}

.upload-form input{

    width:100%;

    padding:16px;

    border-radius:18px;

    border:1px solid #dbeafe;

    background:#fff;

    margin-bottom:16px;
}

.upload-form button{

    width:100%;

    border:none;

    background:
    linear-gradient(135deg,#2563eb,#1d4ed8);

    color:#fff;

    padding:18px;

    border-radius:18px;

    font-size:15px;
    font-weight:900;

    cursor:pointer;

    transition:0.25s;
}

.upload-form button:hover{

    transform:translateY(-2px);

    box-shadow:
    0 18px 35px rgba(37,99,235,0.20);
}

/*
|--------------------------------------------------------------------------
| Empty
|--------------------------------------------------------------------------
*/

.empty{

    background:#fff;

    border-radius:30px;

    padding:70px;

    text-align:center;

    border:1px dashed #cbd5e1;
}

.empty h2{

    font-size:34px;

    margin-bottom:18px;
}

.empty p{

    color:#64748b;

    line-height:1.9;
}

/*
|--------------------------------------------------------------------------
| Responsive
|--------------------------------------------------------------------------
*/

@media(max-width:700px){

    .page{

        padding:20px;
    }

    .page-title{

        font-size:32px;
    }
}

</style>

</head>

<body>

<div class="page">

    <div class="topbar">

        <h1 class="page-title">

            Homework

        </h1>

        <a
href="dashboard.php?course_id=<?php echo $courseId; ?>"
        class="back-btn">

            ← Dashboard

        </a>

    </div>

    <div class="hero">

        <h1>

            Homework Center

        </h1>

        <p>
SELECT hs.homework_id
FROM core_homework_submissions hs
INNER JOIN core_homework h
    ON h.id = hs.homework_id
WHERE hs.student_id = ?
  AND h.course_id = ?
        </p>

    </div>

    <?php if($homeworks): ?>

        <div class="grid">

            <?php foreach($homeworks as $homework): ?>

                <?php
                $submitted =
                    in_array(
                        $homework['id'],
                        $submittedHomework
                    );
                ?>

                <div class="homework-card">

                    <div class="card-header">

                        <h2>

                            <?php
                            echo e(
                                $homework['title']
                            );
                            ?>

                        </h2>

                        <p>

                            <?php
                            echo e(
                                $homework['description']
                            );
                            ?>

                        </p>

                    </div>

                    <div class="card-body">

                        <div class="info-box">

                            <span>
                                Due Date
                            </span>

                            <strong>

                                <?php
                                echo formatDate(
                                    $homework['due_date']
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info-box">

                            <span>
                                Weight
                            </span>

                            <strong>

                                <?php
                                echo e(
                                    $homework['weight']
                                );
                                ?>%

                            </strong>

                        </div>

                        <div class="info-box">

                            <span>
                                Status
                            </span>

                            <strong>

                                <span class="status <?php echo $submitted ? 'submitted' : 'pending'; ?>">

                                    <?php
                                    echo $submitted
                                        ? 'Submitted'
                                        : 'Pending';
                                    ?>

                                </span>

                            </strong>

                        </div>

                        <?php if(!$submitted): ?>

                            <form
                            method="POST"
                            action="../ajax/submit-homework.php"
                            enctype="multipart/form-data"
                            class="upload-form">

                                <?php echo csrfField(); ?>

                                <input
                                type="hidden"
                                name="homework_id"
                                value="<?php echo e($homework['id']); ?>">

                                <input
                                type="file"
                                name="homework_file"
                                required>

                                <button type="submit">

                                    Upload Homework

                                </button>

                            </form>

                        <?php else: ?>

                            <div class="info-box">

                                <span>
                                    Submission
                                </span>

                                <strong>

                                    Completed

                                </strong>

                            </div>

                        <?php endif; ?>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Homework Yet

            </h2>

            <p>

                Homework assignments will appear here
                once your teacher starts publishing tasks.

            </p>

        </div>

    <?php endif; ?>

</div>

</body>
</html>