<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| My Courses
|--------------------------------------------------------------------------
*/

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);

if (!$student) {

    studentLogout();

    redirectTo('login.php');
}

/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/

$courses = [];

if (
    tableExists($pdo, 'core_course_students')
    &&
    tableExists($pdo, 'core_courses')
) {

    $stmt = $pdo->prepare("

        SELECT
            c.*,
            cs.status AS enrollment_status,
            cs.joined_at,
            cs.archive_status

        FROM core_course_students cs

        INNER JOIN core_courses c
            ON c.id = cs.course_id

        WHERE cs.student_id = ?
          AND cs.status = 'active'

        ORDER BY cs.id DESC

    ");

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

    $courses = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

/*
|--------------------------------------------------------------------------
| Pending Requests
|--------------------------------------------------------------------------
*/

$requests = [];

if (
    tableExists($pdo, 'core_course_join_requests')
    &&
    tableExists($pdo, 'core_courses')
) {

    $stmt = $pdo->prepare("

        SELECT
            r.*,
            c.title,
            c.level,
            c.course_code

        FROM core_course_join_requests r

        INNER JOIN core_courses c
            ON c.id = r.course_id

        WHERE r.student_id = ?
          AND r.status IN ('pending','rejected')

        ORDER BY r.id DESC

    ");

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

    $requests = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function courseImage(array $course): string
{
    if (!empty($course['image'])) {
        return '../' . ltrim((string)$course['image'], '/');
    }

    if (!empty($course['image_path'])) {
        return '../' . ltrim((string)$course['image_path'], '/');
    }

    return '';
}

?>

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

<meta charset="UTF-8">

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

<title>
My Courses
</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:32px;
}

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

.topbar{
    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:16px;
    margin-bottom:30px;
}

.page-title{
    font-size:42px;
    font-weight:900;
}

.top-actions{
    display:flex;
    gap:14px;
    flex-wrap:wrap;
}

.top-btn{
    text-decoration:none;
    padding:14px 20px;
    border-radius:18px;
    font-weight:900;
}

.join-btn{
    background:linear-gradient(135deg,#2563eb,#1d4ed8);
    color:#fff;
}

.back-btn{
    background:#fff;
    color:#2563eb;
    border:1px solid #dbeafe;
}

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

.grid{
    display:grid;
    grid-template-columns:
    repeat(auto-fit,minmax(400px,1fr));
    gap:26px;
}

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

.course-card{
    background:#fff;
    border-radius:34px;
    overflow:hidden;
    border:1px solid #e2e8f0;
    box-shadow:
    0 18px 45px rgba(15,23,42,0.05);

    transition:0.25s;
}

.course-card:hover{
    transform:translateY(-5px);
    box-shadow:
    0 28px 70px rgba(15,23,42,0.10);
}

.course-image{
    height:220px;
    background:
    linear-gradient(135deg,#1d4ed8,#0f172a);

    position:relative;
    overflow:hidden;
}

.course-image img{
    width:100%;
    height:100%;
    object-fit:cover;
}

.overlay{
    position:absolute;
    inset:0;
    background:
    linear-gradient(
        to top,
        rgba(15,23,42,0.75),
        transparent
    );
}

.level-badge{
    position:absolute;
    top:18px;
    right:18px;

    background:#fff;
    color:#2563eb;

    padding:10px 16px;

    border-radius:999px;

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

    z-index:2;
}

.course-body{
    padding:28px;
}

.course-title{
    font-size:30px;
    font-weight:900;
    margin-bottom:14px;
}

.course-description{
    color:#64748b;
    line-height:1.9;
    margin-bottom:24px;
}

/*
|--------------------------------------------------------------------------
| Info
|--------------------------------------------------------------------------
*/

.info-grid{
    display:grid;
    gap:14px;
    margin-bottom:26px;
}

.info-box{
    display:flex;
    justify-content:space-between;
    align-items:center;

    padding:16px 18px;

    background:#f8fafc;

    border-radius:18px;
}

.info-box span{
    color:#64748b;
    font-weight:700;
}

.info-box strong{
    font-weight:900;
}

/*
|--------------------------------------------------------------------------
| Actions
|--------------------------------------------------------------------------
*/

.actions{
    display:grid;
    grid-template-columns:repeat(2,1fr);
    gap:14px;
}

.action-btn{
    text-decoration:none;
    text-align:center;

    padding:15px 18px;

    border-radius:18px;

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

    transition:0.25s;
}

.action-btn:hover{
    transform:translateY(-2px);
}

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

    color:#fff;
}

.secondary-btn{
    background:#eff6ff;
    color:#2563eb;
}

/*
|--------------------------------------------------------------------------
| Request Box
|--------------------------------------------------------------------------
*/

.section-title{
    font-size:30px;
    font-weight:900;
    margin:45px 0 20px;
}

.request-box{
    background:#fff;
    border:1px solid #e2e8f0;
    border-radius:24px;
    padding:24px;
    margin-bottom:16px;

    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:20px;
}

.request-box h3{
    font-size:22px;
    margin-bottom:8px;
}

.request-box p{
    color:#64748b;
}

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

.status{
    display:inline-block;

    padding:10px 16px;

    border-radius:999px;

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

.status.active{
    background:#dcfce7;
    color:#16a34a;
}

.status.archived{
    background:#fef3c7;
    color:#b45309;
}

.status.pending{
    background:#fef3c7;
    color:#b45309;
}

.status.rejected{
    background:#fee2e2;
    color:#dc2626;
}

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

.empty{
    background:#fff;
    border:1px dashed #cbd5e1;
    border-radius:34px;
    padding:70px;
    text-align:center;
}

.empty h2{
    font-size:36px;
    margin-bottom:18px;
}

.empty p{
    color:#64748b;
    line-height:2;
    margin-bottom:28px;
}

.empty-btn{
    display:inline-block;
    text-decoration:none;

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

    color:#fff;

    padding:18px 28px;

    border-radius:18px;

    font-weight:900;
}

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

@media(max-width:800px){

    .page{
        padding:20px;
    }

    .topbar{
        flex-direction:column;
        align-items:flex-start;
    }

    .page-title{
        font-size:34px;
    }

    .grid{
        grid-template-columns:1fr;
    }

    .actions{
        grid-template-columns:1fr;
    }

    .request-box{
        flex-direction:column;
        align-items:flex-start;
    }
}

</style>

</head>

<body>

<div class="page">

    <div class="topbar">

        <h1 class="page-title">

            My Courses

        </h1>

        <div class="top-actions">

            <a
            href="join-course.php"
            class="top-btn join-btn">

                Join Course

            </a>

            <a
            href="dashboard.php"
            class="top-btn back-btn">

                ← Dashboard

            </a>

        </div>

    </div>

    <?php if($courses): ?>

        <div class="grid">

            <?php foreach($courses as $course): ?>

                <?php
                $image = courseImage($course);
                $courseId = (int)$course['id'];
                ?>

                <div class="course-card">

                    <div class="course-image">

                        <?php if($image !== ''): ?>

                            <img
                            src="<?php echo e($image); ?>"
                            alt="Course Image">

                        <?php endif; ?>

                        <div class="overlay"></div>

                        <div class="level-badge">

                            <?php
                            echo e(
                                strtoupper(
                                    (string)($course['level'] ?? 'COURSE')
                                )
                            );
                            ?>

                        </div>

                    </div>

                    <div class="course-body">

                        <h2 class="course-title">

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

                        </h2>

                        <p class="course-description">

                            <?php
                            echo e(
                                (string)(
                                    $course['description']
                                    ??
                                    'No description available.'
                                )
                            );
                            ?>

                        </p>

                        <div class="info-grid">

                            <div class="info-box">

                                <span>
                                    Course Code
                                </span>

                                <strong>

                                    <?php
                                    echo e(
                                        $course['course_code']
                                        ?? 'N/A'
                                    );
                                    ?>

                                </strong>

                            </div>

                            <div class="info-box">

                                <span>
                                    Enrollment
                                </span>

                                <strong>

                                    <span class="status <?php echo (($course['archive_status'] ?? 'active') === 'archived') ? 'archived' : 'active'; ?>">

                                        <?php
                                        echo (($course['archive_status'] ?? 'active') === 'archived')
                                            ? 'Archived'
                                            : 'Active';
                                        ?>

                                    </span>

                                </strong>

                            </div>

                        </div>

                        <div class="actions">

                            <a
                            href="materials.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn primary-btn">

                                Materials

                            </a>

                            <a
                            href="exams.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn secondary-btn">

                                Exams

                            </a>

                            <a
                            href="grades.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn secondary-btn">

                                Grades

                            </a>

                            <a
                            href="homework.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn secondary-btn">

                                Homework

                            </a>

                            <a
                            href="attendance.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn secondary-btn">

                                Attendance

                            </a>

                            <a
                            href="shadowing.php?course_id=<?php echo $courseId; ?>"
                            class="action-btn secondary-btn">

                                Shadowing

                            </a>

                        </div>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Courses Yet

            </h2>

            <p>

                You are not enrolled in any course yet.
                Join your official SMI course using
                the course code provided by your teacher.

            </p>

            <a
            href="join-course.php"
            class="empty-btn">

                Join Course

            </a>

        </div>

    <?php endif; ?>

    <?php if($requests): ?>

        <h2 class="section-title">

            Join Requests

        </h2>

        <?php foreach($requests as $request): ?>

            <div class="request-box">

                <div>

                    <h3>

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

                    </h3>

                    <p>

                        Code:
                        <strong>

                            <?php
                            echo e(
                                $request['course_code']
                            );
                            ?>

                        </strong>

                    </p>

                </div>

                <span class="status <?php echo e($request['status']); ?>">

                    <?php
                    echo e(
                        ucfirst(
                            $request['status']
                        )
                    );
                    ?>

                </span>

            </div>

        <?php endforeach; ?>

    <?php endif; ?>

</div>

</body>
</html>