<!doctype html>
<html lang="pt">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= e($title ?? 'Moz Cursos') ?></title>
    <link rel="stylesheet" href="/assets/css/app.css?v=<?= e((string) filemtime(app()->basePath('public/assets/css/app.css'))) ?>">
</head>
<?php
    $currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
    $bodyClasses = [];
    $isAdminSurface = str_starts_with($currentPath, '/admin') || ($currentPath === '/dashboard' && has_role('admin'));
    if (str_starts_with($currentPath, '/admin') || ($currentPath === '/dashboard' && has_role('admin'))) {
        $bodyClasses[] = 'admin-surface';
    }
    $isCreatorSurface = str_starts_with($currentPath, '/manage');
    if ($isCreatorSurface) {
        $bodyClasses[] = 'creator-surface';
    }
?>
<body class="<?= e(implode(' ', $bodyClasses)) ?>">
    <?php
        $unreadNotifications = 0;
        if (auth_check()) {
            $unreadNotifications = (new \App\Services\CommunicationService())->unreadCount((int) current_user()['id']);
        }
        $cartCount = (new \App\Services\CartService())->count();
    ?>
    <header class="topbar">
        <?php if ($isAdminSurface): ?>
            <button class="admin-menu-toggle" type="button" aria-label="Abrir menu" aria-expanded="true" data-admin-menu-toggle>
                <span></span>
                <span></span>
                <span></span>
            </button>
        <?php endif; ?>
        <a class="brand" href="/"><span>Mz</span>Cursos</a>
        <form class="top-search" method="get" action="/courses">
            <input name="search" placeholder="<?= $isAdminSurface ? 'Pesquisar cursos, alunos, pagamentos' : 'Pesquisar cursos' ?>">
        </form>
        <?php if ($isAdminSurface && auth_check()): ?>
            <nav class="nav admin-top-actions">
                <a class="admin-icon-button" href="/notifications" aria-label="Notificacoes">
                    <span></span>
                    <?php if ($unreadNotifications > 0): ?><b><?= e((string) $unreadNotifications) ?></b><?php endif; ?>
                </a>
                <a class="admin-icon-button mail" href="/communication/questions" aria-label="Perguntas">
                    <span></span>
                </a>
                <a class="admin-new-button" href="/manage/courses/create"><i>+</i> Novo curso</a>
            </nav>
        <?php else: ?>
            <nav class="nav">
                <a href="/courses">Cursos</a>
                <a class="nav-pill" href="/cart">Carrinho<?= $cartCount > 0 ? ' (' . e((string) $cartCount) . ')' : '' ?></a>
                <?php if (auth_check()): ?>
                    <a href="/dashboard">Dashboard</a>
                    <?php if (has_role('student')): ?>
                        <details class="nav-dropdown">
                            <summary>Aluno</summary>
                            <div>
                                <a href="/student/courses">Meus cursos</a>
                                <a href="/communication/questions">Perguntas</a>
                                <a href="/payments">Pagamentos</a>
                                <a href="/invoices">Recibos</a>
                                <a href="/certificates">Certificados</a>
                                <a href="/support">Suporte</a>
                            </div>
                        </details>
                    <?php endif; ?>
                    <?php if (in_array(current_user()['role'] ?? '', ['admin', 'instructor', 'institution'], true)): ?>
                        <details class="nav-dropdown">
                            <summary>Ensino</summary>
                            <div>
                                <a href="/manage/courses">Meus cursos</a>
                                <a href="/communication/questions">Perguntas</a>
                                <a href="/earnings">Carteira</a>
                                <a href="/support">Suporte</a>
                            </div>
                        </details>
                    <?php endif; ?>
                    <?php if (has_role('admin')): ?>
                        <details class="nav-dropdown">
                            <summary>Admin</summary>
                            <div>
                                <a href="/admin/users">Utilizadores</a>
                                <a href="/admin/courses">Cursos</a>
                                <a href="/admin/payments">Pagamentos</a>
                                <a href="/admin/invoices">Recibos</a>
                                <a href="/admin/commissions">Comissoes</a>
                                <a href="/admin/certificates">Certificados</a>
                                <a href="/admin/reviews">Avaliacoes</a>
                                <a href="/admin/promotions">Promocoes</a>
                                <a href="/admin/reports">Relatorios</a>
                                <a href="/admin/support">Suporte</a>
                            </div>
                        </details>
                    <?php endif; ?>
                    <a class="nav-pill" href="/notifications">Notificacoes<?= $unreadNotifications > 0 ? ' (' . e((string) $unreadNotifications) . ')' : '' ?></a>
                    <a href="/profile">Perfil</a>
                    <form method="post" action="/logout" class="inline-form">
                        <?= csrf_field() ?>
                        <button class="link-button" type="submit">Sair</button>
                    </form>
                <?php else: ?>
                    <a href="/login">Entrar</a>
                    <a class="nav-strong nav-cta" href="/register">Criar conta</a>
                <?php endif; ?>
            </nav>
        <?php endif; ?>
    </header>
    <?php if ($isAdminSurface): ?>
        <?php
            $adminGroups = [
                'Operacao' => [
                    ['label' => 'Painel', 'abbr' => 'P', 'url' => '/dashboard', 'match' => ['/dashboard']],
                    ['label' => 'Utilizadores', 'abbr' => 'U', 'url' => '/admin/users', 'match' => ['/admin/users']],
                    ['label' => 'Relatorios', 'abbr' => 'RP', 'url' => '/admin/reports', 'match' => ['/admin/reports']],
                    ['label' => 'Suporte', 'abbr' => 'S', 'url' => '/admin/support', 'match' => ['/admin/support']],
                ],
                'Conteudo' => [
                    ['label' => 'Cursos', 'abbr' => 'C', 'url' => '/admin/courses', 'match' => ['/admin/courses', '/admin/categories']],
                    ['label' => 'Certificados', 'abbr' => 'CT', 'url' => '/admin/certificates', 'match' => ['/admin/certificates']],
                    ['label' => 'Avaliacoes', 'abbr' => 'A', 'url' => '/admin/reviews', 'match' => ['/admin/reviews']],
                ],
                'Financeiro' => [
                    ['label' => 'Pagamentos', 'abbr' => 'PG', 'url' => '/admin/payments', 'match' => ['/admin/payments']],
                    ['label' => 'Recibos', 'abbr' => 'R', 'url' => '/admin/invoices', 'match' => ['/admin/invoices']],
                    ['label' => 'Comissoes', 'abbr' => 'CM', 'url' => '/admin/commissions', 'match' => ['/admin/commissions']],
                    ['label' => 'Levantamentos', 'abbr' => 'LV', 'url' => '/admin/withdrawals', 'match' => ['/admin/withdrawals']],
                ],
                'Crescimento' => [
                    ['label' => 'Promocoes', 'abbr' => 'PR', 'url' => '/admin/promotions', 'match' => ['/admin/promotions']],
                ],
                'Sistema' => [
                    ['label' => 'Pagamentos', 'abbr' => 'CFG', 'url' => '/admin/settings/payments', 'match' => ['/admin/settings/payments']],
                ],
            ];
        ?>
        <div class="admin-layout">
            <aside class="admin-sidebar" aria-label="Navegacao administrativa">
                <a class="admin-sidebar-brand" href="/dashboard"><span>Mz</span>Cursos</a>
                <div class="admin-sidebar-head">
                    <span>Admin</span>
                    <strong>Centro de controlo</strong>
                </div>
                <nav class="admin-side-nav">
                    <?php foreach ($adminGroups as $groupName => $links): ?>
                        <div class="admin-nav-group">
                            <span class="admin-nav-label"><?= e($groupName) ?></span>
                            <?php foreach ($links as $link): ?>
                                <?php
                                    $active = false;
                                    foreach ($link['match'] as $match) {
                                        if ($currentPath === $match || str_starts_with($currentPath, $match . '/')) {
                                            $active = true;
                                            break;
                                        }
                                    }
                                ?>
                                <a class="<?= $active ? 'active' : '' ?>" href="<?= e($link['url']) ?>">
                                    <i><?= e($link['abbr']) ?></i>
                                    <span><?= e($link['label']) ?></span>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php endforeach; ?>
                </nav>
                <div class="admin-sidebar-foot">
                    <div>
                        <span>Admin activo</span>
                        <strong><?= e(current_user()['name'] ?? 'Admin') ?></strong>
                    </div>
                    <form method="post" action="/logout">
                        <?= csrf_field() ?>
                        <button type="submit">Sair</button>
                    </form>
                </div>
            </aside>
    <?php endif; ?>
    <main class="shell">
        <?php if ($message = flash('success')): ?>
            <div class="alert success"><?= e((string) $message) ?></div>
        <?php endif; ?>
        <?php if ($message = flash('error')): ?>
            <div class="alert error"><?= e((string) $message) ?></div>
        <?php endif; ?>
