<?php
declare(strict_types=1);

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

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

$pdo = db();

requireTeacher();

$teacher = currentTeacher($pdo);

if (!$teacher) {

    teacherLogout();

    redirectTo('teacher/login.php');
}

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

$courses = [];

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

    $stmt = $pdo->prepare("

        SELECT *
        FROM core_courses

        WHERE teacher_id = ?

        ORDER BY id DESC

    ");

    $stmt->execute([

        $teacher['id']

    ]);

    $courses =
        $stmt->fetchAll();
}

?>

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

<head>

<meta charset="UTF-8">

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

<title>
Teacher 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:35px;
}

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

.topbar{

    display:flex;

    justify-content:space-between;

    align-items:center;

    margin-bottom:30px;
}

.title{

    font-size:42px;

    font-weight:900;
}

.create-btn{

    text-decoration:none;

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

    color:#fff;

    padding:16px 24px;

    border-radius:18px;

    font-weight:900;
}

/*
|--------------------------------------------------------------------------
| 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(360px,1fr));

    gap:24px;
}

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

.card{

    background:#fff;

    border-radius:30px;

    overflow:hidden;

    border:1px solid #e2e8f0;

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

    transition:0.25s;
}

.card:hover{

    transform:translateY(-4px);

    box-shadow:
    0 25px 60px rgba(15,23,42,0.10);
}

.cover{

    height:220px;

    background-size:cover;

    background-position:center;
}

.body{

    padding:28px;
}

.course-title{

    font-size:28px;

    margin-bottom:12px;

    font-weight:900;
}

.course-desc{

    color:#64748b;

    line-height:1.9;

    margin-bottom:24px;
}

.info{

    display:flex;

    justify-content:space-between;

    align-items:center;

    background:#f8fafc;

    border-radius:16px;

    padding:14px 18px;

    margin-bottom:14px;
}

.info span{

    color:#64748b;

    font-weight:700;
}

.info strong{

    font-weight:900;
}

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

.actions{

    display:flex;

    gap:14px;

    margin-top:22px;
}

.action-btn{

    flex:1;

    text-align:center;

    text-decoration:none;

    padding:16px;

    border-radius:18px;

    font-weight:900;
}

.edit{

    background:#eff6ff;

    color:#2563eb;
}

.manage{

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

    color:#fff;
}

/*
|--------------------------------------------------------------------------
| 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;
    }

    .topbar{

        flex-direction:column;

        align-items:flex-start;

        gap:18px;
    }

    .title{

        font-size:34px;
    }

    .hero h1{

        font-size:34px;
    }
}

</style>

</head>

<body>

<div class="page">

    <div class="topbar">

        <h1 class="title">

            My Courses

        </h1>

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

            + Create Course

        </a>

    </div>

    <div class="hero">

        <h1>

            Teacher Courses

        </h1>

        <p>

            Create and manage courses,
            track academic progress,
            organize materials,
            exams,
            attendance,
            and all course systems.

        </p>

    </div>

    <?php if($courses): ?>

        <div class="grid">

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

                <div class="card">

                    <div
                    class="cover"

                    style="background-image:url('../<?php
                    echo e(
                        ($course['image_path'] ?? '') ?: (($course['image'] ?? '') ?: 'assets/images/course-placeholder.jpg')
                    );
                    ?>');">

                    </div>

                    <div class="body">

                        <div class="course-title">

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

                        </div>

                        <div class="course-desc">

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

                        </div>

                        <div class="info">

                            <span>
                                Course Code
                            </span>

                            <strong>

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

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                Start Date
                            </span>

                            <strong>

                                <?php
                                echo formatDate(
                                    $course['start_date']
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                End Date
                            </span>

                            <strong>

                                <?php
                                echo formatDate(
                                    $course['end_date']
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="actions">

                            <a
                            href="edit-course.php?id=<?php echo e($course['id']); ?>"
                            class="action-btn edit">

                                Edit

                            </a>

                            <a
                            href="sessions.php?course=<?php echo e($course['id']); ?>"
                            class="action-btn manage">

                                Manage

                            </a>

                        </div>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Courses Yet

            </h2>

            <p>

                Start by creating your first course
                from the teacher dashboard.

            </p>

        </div>

    <?php endif; ?>

</div>

</body>
</html>