<?php
/**
 * Componentes visuais compartilhados do aplicativo Aura Farming.
 * Mantém o cabeçalho e a navegação principal idênticos em todas as áreas.
 */

if (!function_exists('afUiIcon')) {
    function afUiIcon(string $name, string $class = ''): string
    {
        $attrs = 'class="af-icon ' . esc($class) . '" viewBox="0 0 24 24" aria-hidden="true" focusable="false"';

        return match ($name) {
            'bell' => '<svg ' . $attrs . '><path d="M18 8a6 6 0 0 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9"/><path d="M10 21h4"/></svg>',
            'diamond' => '<svg ' . $attrs . '><path d="m3 8 4-5h10l4 5-9 13L3 8Z"/><path d="m7 3 5 18 5-18M3 8h18"/></svg>',
            'plus' => '<svg ' . $attrs . '><path d="M12 5v14M5 12h14"/></svg>',
            'trophy' => '<svg ' . $attrs . '><path d="M8 21h8M12 17v4M7 4h10v4a5 5 0 0 1-10 0V4Z"/><path d="M7 6H4v2a4 4 0 0 0 4 4M17 6h3v2a4 4 0 0 1-4 4"/></svg>',
            'play' => '<svg ' . $attrs . '><path d="m8 5 11 7-11 7V5Z"/></svg>',
            'fire' => '<svg ' . $attrs . '><path d="M12 22c4 0 7-3 7-7 0-3-2-6-5-9 0 3-2 4-3 5 0-4-2-7-4-9 0 5-3 8-3 13 0 4 4 7 8 7Z"/><path d="M9 17c0 2 1 3 3 3s3-1 3-3c0-1-1-3-3-5-1 2-3 3-3 5Z"/></svg>',
            'monitor' => '<svg ' . $attrs . '><rect x="3" y="4" width="18" height="13" rx="2"/><path d="M8 21h8M12 17v4"/><path d="m10 8 5 3-5 3V8Z"/></svg>',
            'arrow' => '<svg ' . $attrs . '><path d="M5 12h14M13 6l6 6-6 6"/></svg>',
            'broadcast' => '<svg ' . $attrs . '><circle cx="12" cy="12" r="2"/><path d="M8.5 8.5a5 5 0 0 0 0 7M15.5 8.5a5 5 0 0 1 0 7M5.5 5.5a9 9 0 0 0 0 13M18.5 5.5a9 9 0 0 1 0 13"/></svg>',
            'eye' => '<svg ' . $attrs . '><path d="M2 12s4-6 10-6 10 6 10 6-4 6-10 6S2 12 2 12Z"/><circle cx="12" cy="12" r="2.5"/></svg>',
            'target' => '<svg ' . $attrs . '><circle cx="12" cy="12" r="8"/><circle cx="12" cy="12" r="4"/><path d="M12 2v3M22 12h-3"/></svg>',
            'crown' => '<svg ' . $attrs . '><path d="m3 7 4 4 5-7 5 7 4-4-2 11H5L3 7Z"/><path d="M5 21h14"/></svg>',
            'cart' => '<svg ' . $attrs . '><path d="M3 4h2l2 11h10l3-8H6"/><circle cx="9" cy="20" r="1"/><circle cx="17" cy="20" r="1"/></svg>',
            'home' => '<svg ' . $attrs . '><path d="m3 11 9-8 9 8"/><path d="M5 10v11h14V10M9 21v-6h6v6"/></svg>',
            'swords' => '<svg ' . $attrs . '><path d="m14 5 5-2-2 5-8 8-3 1 1-3 7-9ZM10 5 5 3l2 5 3 3M14 14l3 3 3 1-1-3-3-3M5 19l3-3"/></svg>',
            'user' => '<svg ' . $attrs . '><circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/></svg>',
            'search' => '<svg ' . $attrs . '><circle cx="11" cy="11" r="7"/><path d="m20 20-4-4"/></svg>',
            'ticket' => '<svg ' . $attrs . '><path d="M3 7a2 2 0 0 0 2-2h14a2 2 0 0 0 2 2v3a2 2 0 0 0 0 4v3a2 2 0 0 0-2 2H5a2 2 0 0 0-2-2v-3a2 2 0 0 0 0-4V7Z"/><path d="M12 6v2M12 11v2M12 16v2"/></svg>',
            'shield' => '<svg ' . $attrs . '><path d="M12 22s8-4 8-11V5l-8-3-8 3v6c0 7 8 11 8 11Z"/><path d="m9 12 2 2 4-5"/></svg>',
            'edit' => '<svg ' . $attrs . '><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L8 18l-4 1 1-4 11.5-11.5Z"/></svg>',
            'logout' => '<svg ' . $attrs . '><path d="M10 17l5-5-5-5M15 12H3"/><path d="M14 3h5a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-5"/></svg>',
            default => '<svg ' . $attrs . '><circle cx="12" cy="12" r="9"/></svg>',
        };
    }
}

if (!function_exists('afUiAccountSummary')) {
    function afUiAccountSummary(PDO $pdo, array $user): array
    {
        $userId = (int) ($user['id'] ?? 0);

        if (!function_exists('afDiamondsGetBalance') && file_exists(__DIR__ . '/diamond_functions.php')) {
            require_once __DIR__ . '/diamond_functions.php';
        }

        return [
            'notifications' => $userId > 0 && function_exists('contarNotificacoesNaoLidas')
                ? (int) contarNotificacoesNaoLidas($pdo, $userId)
                : 0,
            'diamonds' => $userId > 0 && function_exists('afDiamondsGetBalance')
                ? (int) afDiamondsGetBalance($pdo, $userId)
                : 0,
            'score' => (float) ($user['melhor_pontuacao'] ?? 0),
        ];
    }
}

if (!function_exists('afRenderAppHeader')) {
    function afRenderAppHeader(PDO $pdo, array $user, array $overrides = []): void
    {
        $summary = array_merge(afUiAccountSummary($pdo, $user), $overrides);
        $notifications = max(0, (int) ($summary['notifications'] ?? 0));
        $diamonds = max(0, (int) ($summary['diamonds'] ?? 0));
        $score = (float) ($summary['score'] ?? 0);
        ?>
        <header class="af-home-header af-app-header">
            <div class="af-home-shell af-home-header__inner">
                <a href="<?= SITE_URL ?>/pages/home.php" class="af-brand" aria-label="Aura Farming — Início">
                    <span>AURA</span>
                    <small>FARMING</small>
                </a>

                <div class="af-header-status" aria-label="Resumo da conta">
                    <a href="<?= SITE_URL ?>/pages/notificacoes.php" class="af-status af-status--notification" aria-label="Notificações">
                        <?= afUiIcon('bell') ?>
                        <?php if ($notifications > 0): ?><b><?= min(99, $notifications) ?></b><?php endif; ?>
                    </a>
                    <a href="<?= SITE_URL ?>/pages/comprar-diamantes.php" class="af-status af-status--diamonds" aria-label="Comprar diamantes">
                        <?= afUiIcon('diamond') ?>
                        <strong><?= formatarNumero($diamonds) ?></strong>
                        <span class="af-status-plus"><?= afUiIcon('plus') ?></span>
                    </a>
                    <a href="<?= SITE_URL ?>/pages/perfil.php" class="af-status af-status--score" aria-label="Maior nota pessoal">
                        <?= afUiIcon('trophy') ?>
                        <span><small>MAIOR NOTA</small><strong><?= $score > 0 ? formatarNumero($score) : '0' ?></strong></span>
                    </a>
                </div>
            </div>
        </header>
        <?php
    }
}

if (!function_exists('afRenderBottomNav')) {
    function afRenderBottomNav(string $activePage): void
    {
        $items = [
            ['home', 'home.php', 'home', 'INÍCIO'],
            ['live', 'ao_vivo.php', 'broadcast', 'AO VIVO'],
            ['arena', 'arena.php', 'swords', 'ARENA'],
            ['ranking', 'ranking.php', 'trophy', 'RANKING'],
            ['perfil', 'perfil.php', 'user', 'PERFIL'],
        ];
        ?>
        <nav class="af-home-nav af-app-nav" aria-label="Navegação principal">
            <div class="af-home-nav__inner">
                <?php foreach ($items as [$key, $file, $icon, $label]): ?>
                    <a href="<?= SITE_URL ?>/pages/<?= $file ?>" class="<?= $activePage === $key ? 'is-active' : '' ?>" <?= $activePage === $key ? 'aria-current="page"' : '' ?>>
                        <?= afUiIcon($icon) ?><span><?= $label ?></span>
                    </a>
                <?php endforeach; ?>
            </div>
        </nav>
        <?php
    }
}
