<?php include app()->basePath('app/Views/partials/header.php'); ?>
<?php $promoVideo = promo_video_data($course['promo_video_url'] ?? ''); ?>

<section class="course-detail-hero">
    <div>
        <span class="badge"><?= e($course['category_name']) ?></span>
        <h1><?= e($course['title']) ?></h1>
        <p><?= e($course['subtitle']) ?></p>
        <div class="course-meta wide">
            <span><?= e(level_label($course['level'])) ?></span>
            <span><?= e((string) $course['workload_hours']) ?>h de carga</span>
            <span><?= e((string) $course['students_count']) ?> alunos</span>
            <span><?= e((string) $course['rating']) ?> de 5</span>
        </div>
        <span class="course-author hero-author">Por <?= e($course['owner_name']) ?></span>
    </div>
    <aside class="buy-box">
        <?php if ($promoVideo['type'] === 'file'): ?>
            <div class="course-promo-player">
                <video controls controlsList="nodownload noplaybackrate" disablePictureInPicture preload="metadata" <?= !empty($course['cover_image_url']) ? 'poster="' . e($course['cover_image_url']) . '"' : '' ?> src="<?= e($promoVideo['url']) ?>"></video>
            </div>
        <?php elseif ($promoVideo['type'] === 'embed'): ?>
            <div class="course-promo-player">
                <iframe src="<?= e($promoVideo['url']) ?>" title="Video de apresentacao do curso" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
            </div>
        <?php else: ?>
            <div class="preview-box course-promo-fallback" style="<?= !empty($course['cover_image_url']) ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.08), rgba(0,0,0,.58)), url(' . e($course['cover_image_url']) . ')' : 'background: ' . e($course['cover_color']) ?>">
                <span><?= $promoVideo['type'] === 'link' ? 'Video de apresentacao' : 'Curso gravado' ?></span>
            </div>
        <?php endif; ?>
        <?php if ($promoVideo['type'] !== 'none'): ?>
            <div class="promo-video-caption">
                <strong>Video de apresentacao</strong>
                <span>Veja rapidamente o que vai aprender antes de comprar.</span>
            </div>
        <?php endif; ?>
        <?php if ($promoVideo['type'] === 'link'): ?>
            <a class="ghost-button full" href="<?= e($promoVideo['url']) ?>" target="_blank" rel="noopener">Abrir video de apresentacao</a>
        <?php endif; ?>
        <div class="buy-price">
            <?php if ($course['is_free']): ?>
                Gratis
            <?php elseif (($pricing['promotion_price'] ?? course_effective_price($course)) < (float) $course['price']): ?>
                <span><?= e(money_mzn($pricing['promotion_price'])) ?></span>
                <del><?= e(money_mzn($course['price'])) ?></del>
            <?php else: ?>
                <?= e(money_mzn($course['price'])) ?>
            <?php endif; ?>
        </div>
        <?php if (!empty($pricing['campaign'])): ?>
            <span class="badge pending"><?= e($pricing['campaign']['name']) ?></span>
        <?php endif; ?>
        <?php if (!empty($enrollment)): ?>
            <a class="button full" href="/learn/course?id=<?= e((string) $course['id']) ?>">Continuar curso</a>
        <?php elseif (auth_check() && has_role('student') && !empty($course['is_free'])): ?>
            <form method="post" action="/courses/enroll">
                <?= csrf_field() ?>
                <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                <button class="button full" type="submit">Inscrever-se gratis</button>
            </form>
        <?php elseif (auth_check() && has_role('student')): ?>
            <form method="post" action="/cart/add">
                <?= csrf_field() ?>
                <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                <button class="ghost-button full" type="submit">Adicionar ao carrinho</button>
            </form>
            <form class="form" method="post" action="/checkout">
                <?= csrf_field() ?>
                <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                <label>
                    Cupao de desconto
                    <input name="coupon_code" placeholder="Ex: BEMVINDO10">
                </label>
                <label>
                    Codigo de afiliado
                    <input name="affiliate_code" value="<?= e($affiliateCode) ?>" placeholder="Ex: JOAO123">
                </label>
                <button class="button full" type="submit">Comprar agora</button>
            </form>
        <?php else: ?>
            <a class="button full" href="/login">Entrar para comprar / inscrever-se</a>
        <?php endif; ?>
        <p class="small">Acesso <?= $course['access_type'] === 'lifetime' ? 'vitalicio' : 'por ' . e((string) $course['access_duration_days']) . ' dias' ?>. Materiais disponiveis quando incluidos.</p>
    </aside>
</section>

<section class="detail-grid">
    <div class="panel">
        <h2>Sobre este curso</h2>
        <p><?= e($course['description']) ?></p>

        <h2>O que vai aprender</h2>
        <?php $outcomes = lines_list($course['learning_outcomes'] ?? ''); ?>
        <?php if ($outcomes): ?>
            <ul class="check-list">
                <?php foreach ($outcomes as $outcome): ?>
                    <li><?= e($outcome) ?></li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p>Conteudo a definir pelo instrutor.</p>
        <?php endif; ?>

        <h2>Requisitos</h2>
        <?php $requirements = lines_list($course['requirements'] ?? ''); ?>
        <?php if ($requirements): ?>
            <ul class="plain-list">
                <?php foreach ($requirements as $requirement): ?>
                    <li><?= e($requirement) ?></li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p>Sem requisitos obrigatorios.</p>
        <?php endif; ?>
    </div>
    <div class="panel">
        <h2>Detalhes</h2>
        <div class="detail-list one">
            <div><span>Instrutor/Instituicao</span><strong><?= e($course['owner_name']) ?></strong></div>
            <div><span>Idioma</span><strong><?= e($course['language']) ?></strong></div>
            <div><span>Certificado</span><strong><?= $course['certificate_enabled'] ? 'Sim' : 'Nao' ?></strong></div>
            <div><span>Conclusao minima</span><strong><?= e((string) ($course['completion_percentage_required'] ?? 100)) ?>%</strong></div>
            <div><span>Publico-alvo</span><strong><?= e($course['target_audience'] ?: '-') ?></strong></div>
        </div>
        <?php $includes = lines_list($course['course_includes'] ?? ''); ?>
        <?php if ($includes): ?>
            <h2>Este curso inclui</h2>
            <ul class="plain-list">
                <?php foreach ($includes as $include): ?>
                    <li><?= e($include) ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</section>

<section class="detail-grid reviews-section">
    <div class="panel">
        <h2>Avaliacoes dos alunos</h2>
        <p><?= e((string) $course['rating']) ?> estrelas com <?= e((string) $course['reviews_count']) ?> avaliacao(oes).</p>

        <div class="review-list">
            <?php if (!$reviews): ?>
                <div class="sub-panel">
                    <p>Este curso ainda nao tem avaliacoes.</p>
                </div>
            <?php endif; ?>

            <?php foreach ($reviews as $review): ?>
                <article class="review-card">
                    <div>
                        <strong><?= e($review['student_name']) ?></strong>
                        <span class="muted"><?= e(format_date($review['updated_at'])) ?></span>
                    </div>
                    <div class="stars"><?= e((string) $review['rating']) ?>/5 estrelas</div>
                    <?php if (!empty($review['comment'])): ?>
                        <p><?= e($review['comment']) ?></p>
                    <?php endif; ?>
                </article>
            <?php endforeach; ?>
        </div>
    </div>

    <aside class="panel">
        <h2>Avaliar curso</h2>
        <?php if (!auth_check()): ?>
            <p>Inicie sessao como aluno para avaliar cursos em que esta inscrito.</p>
            <a class="button full" href="/login">Entrar</a>
        <?php elseif (!has_role('student')): ?>
            <p>Apenas alunos podem avaliar cursos.</p>
        <?php elseif (!$enrollment): ?>
            <p>Inscreva-se neste curso para deixar uma avaliacao.</p>
        <?php else: ?>
            <form class="form" method="post" action="/reviews/store">
                <?= csrf_field() ?>
                <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                <label>
                    Nota
                    <select name="rating" required>
                        <?php for ($i = 5; $i >= 1; $i--): ?>
                            <option value="<?= $i ?>" <?= (int) ($ownReview['rating'] ?? 5) === $i ? 'selected' : '' ?>><?= $i ?> estrela<?= $i > 1 ? 's' : '' ?></option>
                        <?php endfor; ?>
                    </select>
                </label>
                <label>
                    Comentario
                    <textarea name="comment" rows="5" maxlength="1000" placeholder="Conte como foi a sua experiencia."><?= e($ownReview['comment'] ?? '') ?></textarea>
                </label>
                <button class="button full" type="submit"><?= $ownReview ? 'Actualizar avaliacao' : 'Publicar avaliacao' ?></button>
            </form>
        <?php endif; ?>
    </aside>
</section>

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