<?php include app()->basePath('app/Views/partials/header.php'); ?>

<section class="learn-shell">
    <aside class="learn-sidebar">
        <a class="muted-link" href="/student/courses">Voltar aos meus cursos</a>
        <h2><?= e($course['title']) ?></h2>
        <div class="progress-wrap">
            <div class="progress-bar"><span style="width: <?= e((string) $progress['percentage']) ?>%"></span></div>
            <strong><?= e((string) $progress['percentage']) ?>%</strong>
        </div>

        <div class="learn-curriculum">
            <?php foreach ($sections as $section): ?>
                <details open>
                    <summary><?= e($section['title']) ?></summary>
                    <?php foreach ($section['lessons'] as $item): ?>
                        <?php
                            $active = (int) $item['id'] === (int) $lesson['id'];
                            $completed = in_array((int) $item['id'], $completedLessonIds ?? [], true);
                        ?>
                        <a class="<?= $active ? 'active' : '' ?> <?= $completed ? 'completed' : '' ?>" href="/learn/lesson?lesson_id=<?= e((string) $item['id']) ?>">
                            <span><?= e($item['title']) ?></span>
                            <small><?= $completed ? 'Concluida' : e(lesson_type_label($item['type'])) ?></small>
                        </a>
                    <?php endforeach; ?>
                </details>
            <?php endforeach; ?>
        </div>
    </aside>

    <article class="learn-main">
        <p class="eyebrow"><?= e(lesson_type_label($lesson['type'])) ?></p>
        <h1><?= e($lesson['title']) ?></h1>
        <?php if (!empty($lesson['description'])): ?>
            <p><?= e($lesson['description']) ?></p>
        <?php endif; ?>

        <?php if ($lesson['type'] === 'video'): ?>
            <div class="secure-player" oncontextmenu="return false">
                <video
                    controls
                    controlsList="nodownload noplaybackrate"
                    disablePictureInPicture
                    preload="metadata"
                    data-learning-video
                    data-enrollment-id="<?= e((string) $enrollment['id']) ?>"
                    data-lesson-id="<?= e((string) $lesson['id']) ?>"
                    data-csrf="<?= e(csrf_token()) ?>"
                    data-start-second="<?= e((string) (int) ($lessonProgress['last_watched_second'] ?? 0)) ?>"
                    src="/secure/video?lesson_id=<?= e((string) $lesson['id']) ?>"></video>
            </div>
            <?php if ((int) ($lessonProgress['last_watched_second'] ?? 0) > 15 && !$isCompleted): ?>
                <p class="resume-note">Vamos retomar aproximadamente no segundo <?= e((string) (int) $lessonProgress['last_watched_second']) ?>.</p>
            <?php endif; ?>
        <?php elseif ($lesson['type'] === 'pdf'): ?>
            <iframe class="pdf-frame" src="/secure/lesson-file?lesson_id=<?= e((string) $lesson['id']) ?>"></iframe>
        <?php else: ?>
            <article class="lesson-text">
                <?= nl2br(e($lesson['text_content'])) ?>
            </article>
        <?php endif; ?>

        <?php if (!empty($lesson['materials'])): ?>
            <section class="sub-panel">
                <h2>Materiais de apoio</h2>
                <div class="material-list">
                    <?php foreach ($lesson['materials'] as $material): ?>
                        <a class="material-link" href="/secure/material?material_id=<?= e((string) $material['id']) ?>">
                            <?= e($material['title']) ?>
                            <span>Baixar</span>
                        </a>
                    <?php endforeach; ?>
                </div>
            </section>
        <?php endif; ?>

        <div class="learn-actions">
            <?php if ($previousLessonId): ?>
                <a class="ghost-button" href="/learn/lesson?lesson_id=<?= e((string) $previousLessonId) ?>">Aula anterior</a>
            <?php endif; ?>

            <form method="post" action="/learn/complete">
                <?= csrf_field() ?>
                <input type="hidden" name="enrollment_id" value="<?= e((string) $enrollment['id']) ?>">
                <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                <button class="button" type="submit"><?= $isCompleted ? 'Aula concluida' : 'Marcar como concluida' ?></button>
            </form>

            <?php if ($nextLessonId): ?>
                <a class="ghost-button" href="/learn/lesson?lesson_id=<?= e((string) $nextLessonId) ?>">Proxima aula</a>
            <?php elseif (!empty($course['final_quiz_required'])): ?>
                <a class="ghost-button" href="/learn/quiz?course_id=<?= e((string) $course['id']) ?>">Fazer teste final</a>
            <?php endif; ?>
        </div>

        <section class="sub-panel discussion-panel">
            <h2>Perguntas desta aula</h2>
            <form class="form" method="post" action="/communication/questions/ask">
                <?= csrf_field() ?>
                <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                <label>
                    Escreva a sua pergunta
                    <textarea name="body" rows="4" maxlength="1500" placeholder="Ex: Nao entendi esta parte da aula..."></textarea>
                </label>
                <button class="button" type="submit">Enviar pergunta</button>
            </form>

            <div class="question-list">
                <?php if (!$questions): ?>
                    <p>Ainda nao ha perguntas nesta aula.</p>
                <?php endif; ?>

                <?php foreach ($questions as $question): ?>
                    <article class="question-card">
                        <div class="question-head">
                            <strong><?= e($question['student_name']) ?></strong>
                            <span class="badge <?= e($question['status']) ?>"><?= $question['status'] === 'answered' ? 'Respondida' : 'Aberta' ?></span>
                        </div>
                        <p><?= nl2br(e($question['body'])) ?></p>
                        <span class="muted"><?= e(format_date($question['created_at'])) ?></span>

                        <?php if (!empty($question['answer'])): ?>
                            <div class="answer-box">
                                <strong><?= e($question['answered_by_name']) ?></strong>
                                <p><?= nl2br(e($question['answer'])) ?></p>
                                <span class="muted"><?= e(format_date($question['answered_at'])) ?></span>
                            </div>
                        <?php endif; ?>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>
    </article>
</section>

<script>
document.addEventListener('contextmenu', function (event) {
    if (event.target.closest('.secure-player')) {
        event.preventDefault();
    }
});
</script>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
