<?php

$user = current_user();

$user_name = isset($user['name']) ? $user['name'] : 'Usuario';
$user_email = isset($user['email']) ? $user['email'] : '';
$user_role = isset($user['role']) ? $user['role'] : 'equipo';
$user_initial = strtoupper(substr($user_name, 0, 1));

$notificationUnreadCount = 0;
try {
    require_once __DIR__ . '/../core/permissions.php';
    require_once __DIR__ . '/../services/notifications/NotificationService.php';
    $notificationService = new NotificationService(db());
    $notificationUnreadCount = $notificationService->unreadCount(current_user_id());
} catch (Throwable $e) {
    $notificationUnreadCount = 0;
}

?>

<header class="topbar">

    <div class="topbar-left">
        <button class="mobile-only" id="sidebarToggle" type="button" aria-label="Abrir menú" style="border: none; background: transparent; padding: 0; cursor: pointer; align-items: center; color: var(--text-primary);">
            <svg xmlns="http://www.w3.org/2000/svg" width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <line x1="3" y1="10" x2="21" y2="10"></line>
                <line x1="3" y1="14" x2="21" y2="14"></line>
            </svg>
        </button>

        <div class="topbar-title-wrap">
            <strong><?php echo htmlspecialchars($page_title, ENT_QUOTES, 'UTF-8'); ?></strong>
        </div>
    </div>

    <div class="topbar-actions">
        <div class="notification-menu" id="notificationMenu">
            <button class="icon-btn notification-bell-btn" id="notificationBellToggle" type="button" aria-label="Abrir notificaciones" aria-expanded="false">
                <i data-lucide="bell"></i>
                <span class="notification-count-badge <?php echo $notificationUnreadCount > 0 ? 'is-visible' : ''; ?>" id="notificationCountBadge" <?php echo $notificationUnreadCount > 0 ? '' : 'hidden aria-hidden="true"'; ?>><?php echo $notificationUnreadCount > 99 ? '99+' : ($notificationUnreadCount > 0 ? (int)$notificationUnreadCount : ''); ?></span>
            </button>
            <div class="notification-dropdown" id="notificationDropdown" aria-hidden="true" hidden>
                <div class="notification-dropdown-header">
                    <div>
                        <strong>Notificaciones</strong>
                        <span id="notificationDropdownSubtitle"><?php echo (int)$notificationUnreadCount; ?> no leídas</span>
                    </div>
                    <button class="btn btn-sm btn-ghost" type="button" id="notificationMarkAllRead">Leer todo</button>
                </div>
                <div class="notification-dropdown-list" id="notificationDropdownList">
                    <div class="notification-loading">Cargando notificaciones...</div>
                </div>
                <div class="notification-dropdown-footer">
                    <a class="btn btn-sm btn-outline" href="/modules/notifications/">Ver centro</a>
                    <a class="btn btn-sm btn-ghost" href="/modules/notifications/?unread=1">No leídas</a>
                </div>
            </div>
        </div>

        <button class="btn btn-sm btn-outline" id="forceSyncFacto" type="button">
            <i data-lucide="refresh-cw"></i>
            <span class="mobile-hidden">Actualizar</span>
        </button>
    </div>

</header>