<?php
use App\Core\Auth;

function pyrk_mobile_member_guard(?array $user): array
{
    if (!$user) {
        header('Location: login-dev.php?source=app');
        exit;
    }

    if (($user['role_name'] ?? '') !== 'member') {
        header('Location: admin/dashboard.php');
        exit;
    }

    return $user;
}

function pyrk_mobile_member_header(string $title, string $subtitle = ''): void
{
    $first = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
    $sub = htmlspecialchars($subtitle, ENT_QUOTES, 'UTF-8');
    echo '<header class="mobile-member-topbar">';
    echo '<a class="mobile-member-brand" href="mobile_home.php" aria-label="Pyrk home"><span>PY</span><strong>Pyrk</strong></a>';
    echo '<div class="mobile-member-heading"><h1>' . $first . '</h1>' . ($sub !== '' ? '<p>' . $sub . '</p>' : '') . '</div>';
    echo '<a class="mobile-member-bell" href="notifications.php" aria-label="Notifications">🔔<span data-mobile-unread-count hidden>0</span></a>';
    echo '</header>';
}

function pyrk_mobile_member_dock(string $active = ''): void
{
    $items = [
        'home' => ['mobile_home.php', '⌂', 'Home'],
        'walks' => ['mobile_walks.php', '🥾', 'Walks'],
        'qr' => ['my_qr.php?source=app', '▣', 'QR'],
        'rewards' => ['mobile_rewards.php', '🎁', 'Rewards'],
        'profile' => ['mobile_profile.php', '👤', 'Profile'],
    ];

    echo '<nav class="mobile-member-dock" aria-label="Member app navigation">';
    foreach ($items as $key => [$href, $icon, $label]) {
        $class = $key === $active ? ' class="is-active' . ($key === 'qr' ? ' is-primary' : '') . '"' : ($key === 'qr' ? ' class="is-primary"' : '');
        echo '<a' . $class . ' href="' . htmlspecialchars($href, ENT_QUOTES, 'UTF-8') . '"><span>' . $icon . '</span><small>' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '</small></a>';
    }
    echo '</nav>';
}
