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

<?php if (has_role('admin')): ?>
    <?php $dashboard = $adminDashboard ?? []; ?>
    <section class="admin-dashboard-shell">
        <div class="admin-dashboard-head premium">
            <div>
                <h1>Ola, <?= e(explode(' ', trim($user['name'] ?? 'Admin'))[0] ?: 'Admin') ?></h1>
                <p>Aqui esta o resumo da sua plataforma.</p>
            </div>
            <div class="admin-head-actions">
                <a class="ghost-button" href="/admin/reports">Ver relatorios</a>
                <a class="button" href="/manage/courses/create">Novo curso</a>
            </div>
        </div>

        <div class="premium-kpi-grid">
            <?php foreach (($dashboard['kpis'] ?? []) as $item): ?>
                <article class="premium-kpi-card <?= e($item['tone']) ?>">
                    <div class="kpi-icon"><span></span></div>
                    <div>
                        <span><?= e($item['label']) ?></span>
                        <strong><?= e($item['value']) ?></strong>
                        <small><?= e($item['trend']) ?> vs. mes anterior</small>
                    </div>
                    <svg viewBox="0 0 160 52" aria-hidden="true">
                        <polyline points="<?= e($item['points']) ?>"></polyline>
                    </svg>
                </article>
            <?php endforeach; ?>
        </div>

        <section class="premium-dashboard-grid">
            <article class="premium-card sales-card">
                <div class="premium-card-head">
                    <h2>Visao geral de vendas</h2>
                    <a href="/admin/reports">Ultimos 30 dias</a>
                </div>
                <svg class="sales-chart" viewBox="0 0 660 210" aria-hidden="true">
                    <path class="chart-fill" d="M8 170 C80 120 150 135 220 78 C292 112 360 86 430 138 C500 78 572 34 644 72 L644 210 L8 210 Z"></path>
                    <polyline points="<?= e($dashboard['sales']['points'] ?? '') ?>"></polyline>
                    <line x1="430" y1="0" x2="430" y2="210"></line>
                    <circle cx="430" cy="138" r="8"></circle>
                </svg>
                <div class="chart-tooltip"><span>Receita</span><strong><?= e($dashboard['sales']['total'] ?? money_mzn(0)) ?></strong></div>
            </article>

            <article class="premium-card status-card">
                <div class="premium-card-head">
                    <h2>Alunos por status</h2>
                </div>
                <div class="donut-wrap">
                    <div class="donut"><strong><?= e($summary[0]['value'] ?? '0') ?></strong><span>Total</span></div>
                    <div class="status-list">
                        <?php foreach (($dashboard['status'] ?? []) as $status): ?>
                            <div><i style="background: <?= e($status['color']) ?>"></i><span><?= e($status['label']) ?></span><strong><?= e((string) $status['value']) ?></strong></div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </article>

            <article class="premium-card activity-card">
                <div class="premium-card-head">
                    <h2>Actividades recentes</h2>
                    <a href="/notifications">Ver tudo</a>
                </div>
                <div class="activity-list">
                    <?php foreach (($dashboard['recentActivity'] ?? []) as $activity): ?>
                        <div class="activity-item">
                            <i></i>
                            <div>
                                <strong><?= e(str_replace('_', ' ', (string) ($activity['action'] ?? 'actividade'))) ?></strong>
                                <span><?= e($activity['description'] ?? '') ?></span>
                            </div>
                            <small><?= e(format_date($activity['created_at'] ?? '')) ?></small>
                        </div>
                    <?php endforeach; ?>
                </div>
            </article>
        </section>

        <section class="premium-bottom-grid">
            <article class="premium-card recent-courses-card">
                <div class="premium-card-head">
                    <h2>Cursos recentes</h2>
                    <a href="/admin/courses">Ver todos</a>
                </div>
                <div class="premium-table">
                    <div class="premium-table-row head"><span>Curso</span><span>Instrutor</span><span>Receita</span><span>Status</span></div>
                    <?php foreach (($dashboard['recentCourses'] ?? []) as $course): ?>
                        <div class="premium-table-row">
                            <span><i style="background: <?= e($course['cover_color'] ?? '#a435f0') ?>"></i><?= e($course['title']) ?></span>
                            <span><?= e($course['owner_name'] ?? 'Moz Cursos') ?></span>
                            <span><?= e(empty($course['is_free']) ? money_mzn($course['price'] ?? 0) : 'Gratis') ?></span>
                            <span><b class="<?= e($course['status'] ?? 'draft') ?>"><?= e(course_status_label($course['status'] ?? 'draft')) ?></b></span>
                        </div>
                    <?php endforeach; ?>
                </div>
            </article>

            <article class="premium-card popular-card">
                <div class="premium-card-head">
                    <h2>Cursos mais populares</h2>
                    <a href="/admin/reports">Ver relatorio</a>
                </div>
                <div class="popular-list">
                    <?php foreach (($dashboard['popularCourses'] ?? []) as $index => $item): ?>
                        <?php
                            $course = $item['course'];
                            $percent = max(36, 88 - ($index * 11));
                        ?>
                        <div class="popular-item">
                            <i style="background: <?= e($course['cover_color'] ?? '#a435f0') ?>"></i>
                            <div>
                                <strong><?= e($course['title'] ?? 'Curso') ?></strong>
                                <span><?= e((string) $item['sales']) ?> venda(s)</span>
                                <em><b style="width: <?= e((string) $percent) ?>%"></b></em>
                            </div>
                            <small><?= e((string) $percent) ?>%</small>
                        </div>
                    <?php endforeach; ?>
                </div>
            </article>
        </section>
    </section>
<?php else: ?>
    <section class="panel dashboard-hero">
        <div class="page-head">
            <div>
                <p class="eyebrow">Dashboard</p>
                <h1>Bem-vindo, <?= e($user['name']) ?></h1>
                <p><?= e(role_label($user['role'])) ?> - <?= e(status_label($user['status'])) ?> - Plataforma em MZN</p>
            </div>
            <a class="ghost-button" href="/profile">Editar perfil</a>
        </div>

        <div class="stats">
            <?php foreach ($summary as $item): ?>
                <div class="stat">
                    <span><?= e($item['label']) ?></span>
                    <strong><?= e($item['value']) ?></strong>
                    <small><?= e($item['hint']) ?></small>
                </div>
            <?php endforeach; ?>
        </div>
    </section>

    <section class="dashboard-grid">
        <?php foreach ($shortcuts as $shortcut): ?>
            <a class="module-card" href="<?= e($shortcut['url']) ?>">
                <strong><?= e($shortcut['label']) ?></strong>
                <span><?= e($shortcut['text']) ?></span>
            </a>
        <?php endforeach; ?>
    </section>

    <section class="panel ops-panel">
        <div class="page-head">
            <div>
                <p class="eyebrow">Estado da plataforma</p>
                <h2>Base consolidada</h2>
                <p>Os modulos principais ja partilham o mesmo fluxo: curso, acesso, pagamento, progresso, certificado, comunicacao e vendas.</p>
            </div>
            <span class="badge active">Operacional</span>
        </div>
    </section>
<?php endif; ?>

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