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

<?php
    $courses = $courses ?? [];
    $freeCourses = $freeCourses ?? [];
    $paidCourses = $paidCourses ?? [];
    $categories = $categories ?? [];
    $featured = $featuredCourse ?? ($courses[0] ?? null);
    $stats = $courseStats ?? [
        'total' => count($courses),
        'free' => count($freeCourses),
        'paid' => count($paidCourses),
        'students' => 0,
    ];
    $categoryCards = array_slice($categories, 0, 4);
?>

<div class="home-page">
    <section class="home-hero home-hero-v2 serious-hero">
        <div class="home-hero-content">
            <p class="eyebrow">Moz Cursos para alunos, profissionais e instituicoes</p>
            <h1>Cursos online para trabalhar melhor, vender mais e certificar competencias.</h1>
            <p>Uma plataforma de aprendizagem pratica em MZN, com cursos gravados, progresso guardado, pagamentos locais e certificados validaveis.</p>

            <form class="hero-search" method="get" action="/courses">
                <input name="search" placeholder="Pesquisar Excel, vendas, tecnologia, design...">
                <button class="button" type="submit">Encontrar cursos</button>
            </form>

            <div class="hero-actions">
                <a class="button" href="/courses">Explorar catalogo</a>
                <?php if (auth_check()): ?>
                    <a class="ghost-button light" href="/dashboard">Continuar no painel</a>
                <?php else: ?>
                    <a class="ghost-button light" href="/register">Criar conta gratis</a>
                <?php endif; ?>
            </div>

            <div class="hero-proof">
                <span><strong><?= e((string) ($stats['total'] ?? 0)) ?>+</strong> cursos publicados</span>
                <span><strong><?= e((string) ($stats['students'] ?? 0)) ?>+</strong> inscricoes</span>
                <span><strong>MZN</strong> pagamentos locais</span>
            </div>
        </div>

        <aside class="hero-showcase serious-showcase" aria-label="Destaques da plataforma">
            <div class="hero-insight-card">
                <span>Aprendizagem estruturada</span>
                <strong>Aulas, progresso, perguntas e certificado no mesmo percurso.</strong>
            </div>

            <?php if ($featured): ?>
                <?php
                    $featuredCover = !empty($featured['cover_image_url'])
                        ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.04), rgba(0,0,0,.62)), url(' . e((string) $featured['cover_image_url']) . ')'
                        : 'background: ' . e((string) ($featured['cover_color'] ?? '#0f766e'));
                    $featuredPrice = !empty($featured['is_free']) ? 'Gratis' : money_mzn(course_effective_price($featured));
                ?>
                <a class="hero-course-card" href="/courses/show?id=<?= e((string) $featured['id']) ?>">
                    <span class="hero-course-image" style="<?= $featuredCover ?>"></span>
                    <span class="hero-course-body">
                        <small><?= e((string) ($featured['category_name'] ?? 'Curso em destaque')) ?></small>
                        <strong><?= e((string) $featured['title']) ?></strong>
                        <em><?= e((string) ($featured['owner_name'] ?? 'Moz Cursos')) ?></em>
                        <span class="hero-course-meta">
                            <b><?= e($featuredPrice) ?></b>
                            <i><?= e((string) ($featured['rating'] ?? 0)) ?>/5</i>
                        </span>
                    </span>
                </a>
            <?php endif; ?>

            <div class="hero-mini-grid">
                <div><strong>Certificados</strong><span>Validacao publica</span></div>
                <div><strong>Pagamentos</strong><span>M-Pesa, e-Mola e mKesh</span></div>
                <div><strong>Mobile</strong><span>Progresso guardado</span></div>
            </div>
        </aside>
    </section>

    <section class="home-logo-strip serious-strip">
        <span>Uma experiencia pensada para Mocambique</span>
        <strong>M-Pesa</strong>
        <strong>e-Mola</strong>
        <strong>mKesh</strong>
        <strong>Certificados PDF</strong>
    </section>

    <section class="home-section home-category-feature">
        <div class="catalog-head">
            <div>
                <p class="eyebrow">Categorias</p>
                <h2>Aprenda por area de interesse.</h2>
            </div>
            <a class="ghost-button" href="/courses">Ver catalogo</a>
        </div>

        <div class="learning-path-grid serious-category-grid">
            <?php if ($categoryCards): ?>
                <?php foreach ($categoryCards as $category): ?>
                    <a href="/courses?category_id=<?= e((string) $category['id']) ?>">
                        <span class="category-mark"><?= e(strtoupper(substr((string) $category['name'], 0, 2))) ?></span>
                        <strong><?= e((string) $category['name']) ?></strong>
                        <span><?= e((string) ($category['description'] ?? 'Cursos praticos para evoluir com foco.')) ?></span>
                    </a>
                <?php endforeach; ?>
            <?php else: ?>
                <a href="/courses?search=excel"><span class="category-mark">EX</span><strong>Produtividade</strong><span>Ferramentas para trabalho e relatorios.</span></a>
                <a href="/courses?search=vendas"><span class="category-mark">VE</span><strong>Vendas</strong><span>Clientes, oferta e crescimento comercial.</span></a>
                <a href="/courses?search=tecnologia"><span class="category-mark">TI</span><strong>Tecnologia</strong><span>Competencias digitais para o mercado.</span></a>
                <a href="/courses?search=design"><span class="category-mark">DE</span><strong>Design</strong><span>Projectos visuais e criatividade aplicada.</span></a>
            <?php endif; ?>
        </div>
    </section>

    <section class="home-section">
        <div class="catalog-head">
            <div>
                <p class="eyebrow">Cursos em destaque</p>
                <h2>Formacoes prontas para comecar.</h2>
                <span>Escolha um curso, acompanhe o progresso e avance no seu ritmo.</span>
            </div>
            <div class="rail-controls">
                <button type="button" data-rail-prev aria-label="Anterior">&lt;</button>
                <button type="button" data-rail-next aria-label="Seguinte">&gt;</button>
            </div>
        </div>

        <?php if ($courses): ?>
            <div class="course-rail" data-rail>
                <?php foreach ($courses as $course): ?>
                    <?php
                        $coverStyle = !empty($course['cover_image_url'])
                            ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.04), rgba(0,0,0,.58)), url(' . e((string) $course['cover_image_url']) . ')'
                            : 'background: ' . e((string) ($course['cover_color'] ?? '#111827'));
                        $priceLabel = !empty($course['is_free']) ? 'Gratis' : money_mzn(course_effective_price($course));
                    ?>
                    <article class="course-card premium-course-card serious-course-card">
                        <a class="course-cover" style="<?= $coverStyle ?>" href="/courses/show?id=<?= e((string) $course['id']) ?>">
                            <span><?= e((string) ($course['category_name'] ?? 'Curso')) ?></span>
                            <?php if (!empty($course['certificate_enabled'])): ?>
                                <small>Certificado</small>
                            <?php endif; ?>
                        </a>
                        <div class="course-body">
                            <div class="course-meta-top">
                                <span><?= e((string) ($course['rating'] ?? 0)) ?>/5</span>
                                <span><?= e((string) ($course['students_count'] ?? 0)) ?> alunos</span>
                            </div>
                            <a class="course-title" href="/courses/show?id=<?= e((string) $course['id']) ?>"><?= e((string) $course['title']) ?></a>
                            <p><?= e((string) ($course['subtitle'] ?? 'Curso pratico com aulas gravadas e acesso online.')) ?></p>
                            <span class="course-author"><?= e((string) ($course['owner_name'] ?? 'Moz Cursos')) ?></span>
                            <div class="course-card-footer">
                                <div class="course-price">
                                    <?= e($priceLabel) ?>
                                    <?php if (empty($course['is_free']) && !empty($course['discount_price'])): ?>
                                        <del><?= e(money_mzn($course['price'])) ?></del>
                                    <?php endif; ?>
                                </div>
                                <a class="mini-button" href="/courses/show?id=<?= e((string) $course['id']) ?>">Ver curso</a>
                            </div>
                        </div>
                    </article>
                <?php endforeach; ?>
                <?php if (count($courses) < 4): ?>
                    <article class="catalog-discovery-card">
                        <span>Catalogo completo</span>
                        <strong>Encontre a formacao certa para o seu proximo passo.</strong>
                        <p>Pesquise por categoria, preco ou objectivo e compare cursos publicados na plataforma.</p>
                        <a class="mini-button" href="/courses">Abrir catalogo</a>
                    </article>
                <?php endif; ?>
            </div>
        <?php else: ?>
            <div class="home-empty-state">
                <strong>Nenhum curso publicado ainda.</strong>
                <span>Assim que existirem cursos aprovados, eles aparecem aqui.</span>
            </div>
        <?php endif; ?>
    </section>

    <section class="home-section home-split-feature serious-learning-feature">
        <div>
            <p class="eyebrow">Experiencia de aprendizagem</p>
            <h2>Do primeiro acesso ao certificado, tudo no mesmo fluxo.</h2>
            <p>O aluno encontra o curso, compra ou se inscreve, assiste aulas, faz perguntas, acompanha progresso e emite certificado quando o curso permitir.</p>
            <div class="feature-checks">
                <span>Player com progresso</span>
                <span>Materiais de apoio</span>
                <span>Perguntas e respostas</span>
                <span>Certificado validavel</span>
            </div>
        </div>
        <div class="learning-preview-card">
            <div class="preview-video">
                <span></span>
            </div>
            <div class="preview-lessons">
                <strong>Modulo 1: Fundamentos</strong>
                <span style="width: 88%"></span>
                <strong>Modulo 2: Pratica guiada</strong>
                <span style="width: 63%"></span>
                <strong>Modulo 3: Projecto final</strong>
                <span style="width: 34%"></span>
            </div>
        </div>
    </section>

    <?php if ($freeCourses): ?>
        <section class="home-section">
            <div class="catalog-head">
                <div>
                    <p class="eyebrow">Comece sem pagar</p>
                    <h2>Cursos gratis para experimentar.</h2>
                </div>
                <a class="ghost-button" href="/courses?pricing=free">Ver gratis</a>
            </div>
            <div class="course-rail compact-rail" data-rail>
                <?php foreach ($freeCourses as $course): ?>
                    <?php
                        $freeCoverStyle = !empty($course['cover_image_url'])
                            ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.06), rgba(0,0,0,.56)), url(' . e((string) $course['cover_image_url']) . ')'
                            : 'background: ' . e((string) ($course['cover_color'] ?? '#0f766e'));
                    ?>
                    <article class="course-card serious-course-card">
                        <a class="course-cover" style="<?= $freeCoverStyle ?>" href="/courses/show?id=<?= e((string) $course['id']) ?>">
                            <span><?= e((string) ($course['category_name'] ?? 'Curso')) ?></span>
                        </a>
                        <div class="course-body">
                            <a class="course-title" href="/courses/show?id=<?= e((string) $course['id']) ?>"><?= e((string) $course['title']) ?></a>
                            <span class="course-author"><?= e((string) ($course['owner_name'] ?? 'Moz Cursos')) ?></span>
                            <div class="course-card-footer">
                                <div class="course-price">Gratis</div>
                                <a class="mini-button" href="/courses/show?id=<?= e((string) $course['id']) ?>">Abrir</a>
                            </div>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endif; ?>

    <section class="home-section how-it-works serious-steps">
        <div class="catalog-head">
            <div>
                <p class="eyebrow">Como funciona</p>
                <h2>Simples para aprender. Profissional para gerir.</h2>
            </div>
        </div>
        <div class="steps-grid">
            <div><i>01</i><strong>Escolha um curso</strong><span>Pesquise por area, categoria, preco ou objectivo.</span></div>
            <div><i>02</i><strong>Pague em MZN</strong><span>Use pagamentos locais e envie comprovativo quando necessario.</span></div>
            <div><i>03</i><strong>Assista no seu ritmo</strong><span>Progresso guardado, materiais e perguntas no curso.</span></div>
            <div><i>04</i><strong>Receba certificado</strong><span>Quando o curso permitir, emita PDF com codigo de validacao.</span></div>
        </div>
    </section>

    <section class="creator-cta creator-cta-v2 serious-creator-cta">
        <div>
            <p class="eyebrow">Para instrutores e instituicoes</p>
            <h2>Publique cursos com uma vitrine mais confiavel.</h2>
            <p>Organize aulas, acompanhe alunos, responda perguntas, controle vendas, comissoes e pedidos de levantamento.</p>
        </div>
        <div class="creator-actions">
            <a class="button" href="/register">Criar conta de instrutor</a>
            <a class="ghost-button light" href="/courses">Ver catalogo</a>
        </div>
    </section>

    <section class="home-section trust-section trust-section-v2 serious-trust">
        <div><strong>Alunos</strong><span>Compra simples, progresso guardado e certificados.</span></div>
        <div><strong>Instrutores</strong><span>Curso, aulas, alunos, vendas e carteira.</span></div>
        <div><strong>Instituicoes</strong><span>Formacoes publicadas com certificados por curso.</span></div>
    </section>
</div>

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