<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Study Materials
|--------------------------------------------------------------------------
*/

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

/*
|--------------------------------------------------------------------------
| Materials
|--------------------------------------------------------------------------
*/

$materials = [];

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

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

$stmt->execute([$courseId]);

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

?>

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

<head>

<meta charset="UTF-8">

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

<title>
Study Materials
</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:42px;

    margin-bottom:30px;

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

.hero h1{

    font-size:46px;

    margin-bottom:16px;

    font-weight:900;
}

.hero p{

    color:#dbeafe;

    line-height:2;
}

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

.grid{

    display:grid;

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

    gap:24px;
}

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

.card-header{

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

    color:#fff;

    padding:28px;
}

.card-header h2{

    font-size:28px;

    margin-bottom:10px;

    font-weight:900;
}

.card-header p{

    color:#dbeafe;

    line-height:1.9;
}

.card-body{

    padding:28px;
}

.info{

    display:flex;

    justify-content:space-between;

    align-items:center;

    background:#f8fafc;

    border-radius:16px;

    padding:15px 18px;

    margin-bottom:14px;
}

.info span{

    color:#64748b;

    font-weight:700;
}

.info strong{

    font-weight:800;
}

/*
|--------------------------------------------------------------------------
| Buttons
|--------------------------------------------------------------------------
*/

.download-btn{

    display:block;

    text-align:center;

    text-decoration:none;

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

    color:#fff;

    padding:18px;

    border-radius:18px;

    font-weight:900;

    margin-top:22px;

    transition:0.25s;
}

.download-btn: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;
    }

    .hero h1{

        font-size:34px;
    }
}

</style>

</head>

<body>

<div class="page">

    <div class="topbar">

        <h1 class="page-title">

            Study Materials

        </h1>

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

            ← Back to Course

        </a>

    </div>

    <div class="hero">

        <h1>

            Course Materials

        </h1>

        <p>
Access study materials,
PDFs,
worksheets,
vocabulary packs,
and academic resources for
<?php echo e((string)$course['title']); ?>.

        </p>

    </div>

    <?php if($materials): ?>

        <div class="grid">

            <?php foreach($materials as $material): ?>

                <div class="card">

                    <div class="card-header">

                        <h2>

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

                        </h2>

                        <p>

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

                        </p>

                    </div>

                    <div class="card-body">

                        <div class="info">

                            <span>
                                Material Type
                            </span>

                            <strong>

                                <?php
                                echo e(
                                    match(strtolower($material['material_type'] ?? '')) {
    'pdf' => 'PDF Document',
    'video' => 'Video Lesson',
    'audio' => 'Audio Material',
    'book' => 'Course Book',
    'worksheet' => 'Worksheet',
    default => 'Study Material'
}
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                Uploaded
                            </span>

                            <strong>

                                <?php
                                echo formatDate(
                                    $material['created_at']
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                Access
                            </span>

                            <strong>

                                Student Access

                            </strong>

                        </div>

<?php if(
    !empty($material['external_link'])
    ||
    !empty($material['file_path'])
): ?>

<a
href="<?php echo !empty($material['external_link']) ? e($material['external_link']) : '../' . e(ltrim((string)$material['file_path'], '/')); ?>"
target="_blank"
class="download-btn">

    Open Material

</a>

<?php else: ?>

<div
class="download-btn"
style="
background:#e2e8f0;
color:#64748b;
cursor:not-allowed;
">

    Material Unavailable

</div>

<?php endif; ?>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Materials Yet

            </h2>

            <p>

                Study materials will appear here
                once your teacher uploads them
                to the course system.

            </p>

        </div>

    <?php endif; ?>

</div>

</body>
</html>