<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Shadowing System
|--------------------------------------------------------------------------
*/

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');
}
/*
|--------------------------------------------------------------------------
| Shadowing Lessons
|--------------------------------------------------------------------------
*/

$shadowingLessons = safeFetchAll($pdo, "
    SELECT id, course_id, teacher_id, title, description, youtube_link AS youtube_url,
           shadowing_level AS lesson_order, points, created_at
    FROM core_shadowing
    WHERE course_id = ?
    ORDER BY shadowing_level ASC, id ASC
", [$courseId]);

$completedLessons = [];
if (tableExists($pdo, 'core_shadowing_submissions')) {
    $submissionStmt = $pdo->prepare("
        SELECT COALESCE(NULLIF(ss.lesson_id,0),NULLIF(ss.task_id,0)) AS lesson_id
        FROM core_shadowing_submissions ss
        INNER JOIN core_shadowing sh ON sh.id = COALESCE(NULLIF(ss.lesson_id,0),NULLIF(ss.task_id,0))
        WHERE ss.student_id = ? AND sh.course_id = ?
          AND ss.status IN ('submitted','approved','reviewed')
    ");
    $submissionStmt->execute([(int)$student['id'], $courseId]);
    $completedLessons = array_map('intval', array_column($submissionStmt->fetchAll(PDO::FETCH_ASSOC), 'lesson_id'));
}

?>

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

<head>

<meta charset="UTF-8">

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

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

    gap:24px;
}

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

.card{

    background:#fff;

    border-radius:30px;

    overflow:hidden;

    border:1px solid #e2e8f0;

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

    position:relative;
}

.card-header{

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

    color:#fff;

    padding:28px;
}

.card-header h2{

    font-size:30px;

    margin-bottom:12px;

    font-weight:900;
}

.card-header p{

    color:#dbeafe;

    line-height:1.9;
}

.card-body{

    padding:28px;
}

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

.status{

    display:inline-block;

    padding:10px 16px;

    border-radius:999px;

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

    margin-bottom:20px;
}

.status.completed{

    background:#dcfce7;

    color:#16a34a;
}

.status.locked{

    background:#fef2f2;

    color:#dc2626;
}

.status.available{

    background:#eff6ff;

    color:#2563eb;
}

/*
|--------------------------------------------------------------------------
| Video
|--------------------------------------------------------------------------
*/

.video-btn{

    display:block;

    text-align:center;

    text-decoration:none;

    background:#ef4444;

    color:#fff;

    padding:18px;

    border-radius:18px;

    font-weight:900;

    margin-bottom:18px;

    transition:0.25s;
}

.video-btn:hover{

    transform:translateY(-2px);

    box-shadow:
    0 18px 35px rgba(239,68,68,0.20);
}

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

.form textarea{

    width:100%;

    min-height:120px;

    border-radius:18px;

    border:1px solid #dbeafe;

    padding:18px;

    resize:vertical;

    margin-bottom:16px;

    font-family:inherit;
}

.form textarea:focus{

    outline:none;

    border-color:#2563eb;

    box-shadow:
    0 0 0 5px rgba(37,99,235,0.10);
}

.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;
}

.form button:hover{

    transform:translateY(-2px);

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

/*
|--------------------------------------------------------------------------
| Locked
|--------------------------------------------------------------------------
*/

.locked-box{

    text-align:center;

    padding:35px;

    background:#fef2f2;

    border-radius:22px;

    color:#dc2626;

    font-weight:800;
}

/*
|--------------------------------------------------------------------------
| 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">

            Shadowing System

        </h1>

        <a
        href="my-courses.php"
        class="back-btn">

          ← My Courses

        </a>

    </div>

    <div class="hero">

    <h1>

        Shadowing Training

    </h1>

    <p>

        Watch the shadowing video carefully,
        repeat the sentences,
        record your voice externally,
        then submit your recording link
        to unlock the next lesson.

    </p>

</div>

    <?php if($shadowingLessons): ?>

        <div class="grid">

            <?php foreach($shadowingLessons as $index => $lesson): ?>

                <?php

                $completed =
                    in_array(
                        $lesson['id'],
                        $completedLessons
                    );

                $previousCompleted =
                    $index === 0
                    ||
                    in_array(
                        $shadowingLessons[$index - 1]['id'],
                        $completedLessons
                    );

                $locked =
                    !$previousCompleted;

                ?>

                <div class="card">

                    <div class="card-header">

                        <h2>

                            Shadowing
                            <?php
                            echo e(
                                $lesson['lesson_order']
                            );
                            ?>

                        </h2>

                        <p>

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

                        </p>

                    </div>

                    <div class="card-body">

                        <?php if($completed): ?>

                            <div class="status completed">

                                Completed

                            </div>

                        <?php elseif($locked): ?>

                            <div class="status locked">

                                Locked

                            </div>

                        <?php else: ?>

                            <div class="status available">

                                Available

                            </div>

                        <?php endif; ?>

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

                            <a
                            href="<?php echo e($lesson['youtube_url']); ?>"
                            target="_blank"
                            class="video-btn">

                                Open YouTube Video

                            </a>

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

                                <form
                                method="POST"
                                action="../ajax/submit-shadowing.php"
                                class="form">

                                    <?php echo csrfField(); ?>

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

                                    <textarea
                                    name="recording_link"
                                    placeholder="Paste your voice recording link here..."
                                    required></textarea>

                                    <button type="submit">

                                        Submit Recording

                                    </button>

                                </form>

                            <?php endif; ?>

                        <?php else: ?>

                            <div class="locked-box">

                                Complete the previous
                                shadowing lesson first
                                to unlock this stage.

                            </div>

                        <?php endif; ?>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Shadowing Lessons Yet

            </h2>

            <p>

                Your teacher has not added
                shadowing lessons yet.

            </p>

        </div>

    <?php endif; ?>

</div>

</body>
</html>