<?php
if (!defined('ABSPATH')) exit;

use BLR\Backline\Modules\Notifications\Module as NotificationsModule;

$user = wp_get_current_user();

$avatar_id = (int) get_user_meta((int) $user->ID, 'blr_ba_avatar_id', true);
$avatar_url = '';
if ($avatar_id > 0) {
  $avatar_url = (string) wp_get_attachment_image_url($avatar_id, 'thumbnail');
}
if ($avatar_url === '') {
  $avatar_url = defined('BLR_BA_URL') ? BLR_BA_URL . 'assets/blr-ba/img/placeholder-image.png' : '';
}

$notification_items = [];
$notification_unread_count = 0;
$can_view_notifications = \BLR\Backline\Core\SettingsPage::is_module_enabled('notifications') && (current_user_can('manage_options') || current_user_can('blr_ba_view_notifications'));
$can_view_settings = \BLR\Backline\Core\SettingsPage::can_view_frontend_settings();
$can_view_users = current_user_can('manage_options') || current_user_can('blr_ba_view_dashboard');
$dashboard_base_url = home_url('/' . ($ctx['dashboard_slug'] ?? 'blr-backline') . '/');

if ((int) $user->ID > 0 && $can_view_notifications) {
  NotificationsModule::sync_user_notifications((int) $user->ID, false);
  $notification_items = NotificationsModule::list_for_user((int) $user->ID, [
    'limit' => 8,
    'include_read' => true,
  ], false);
  $notification_unread_count = NotificationsModule::unread_count((int) $user->ID, false);
}
?>
<nav class="navbar navbar-expand navbar-light navbar-bg">
    <button type="button" class="sidebar-toggle d-flex js-sidebar-toggle border-0 bg-transparent p-0" aria-label="<?php echo esc_attr__('Toggle sidebar', 'blr-backline'); ?>">
    <i class="hamburger align-self-center"></i>
  </button>

  <div class="navbar-collapse collapse">
    <ul class="navbar-nav navbar-align">
      <li class="nav-item">
        <button
          type="button"
          class="nav-link d-flex align-items-center border-0 bg-transparent blr-ba-theme-toggle mt-2 me-2"
          data-blr-theme-toggle
          aria-pressed="false"
          aria-label="<?php echo esc_attr__('Switch to dark mode', 'blr-backline'); ?>"
          title="<?php echo esc_attr__('Toggle light and dark mode', 'blr-backline'); ?>"
        >
          <i id="blr-theme-toggle-icon" data-feather="moon"></i>
        </button>
      </li>

      <?php if ($can_view_notifications) : ?>
      <li class="nav-item dropdown me-2 pt-2">
      <button
        type="button"
        id="blr-ba-alerts-toggle"
        class="nav-link dropdown-toggle border-0 bg-transparent px-2"
        data-bs-toggle="dropdown"
        data-bs-auto-close="true"
        aria-expanded="false"
        aria-label="<?php echo esc_attr__('Notifications', 'blr-backline'); ?>"
      >
          <div class="position-relative d-flex align-items-center">
            <i class="align-middle" data-feather="bell"></i>
            <?php if ($notification_unread_count > 0) : ?>
              <span class="indicator badge bg-danger rounded-pill position-absolute top-0 start-100 translate-middle blr-ba-notification-count">
                <?php echo esc_html(number_format_i18n($notification_unread_count)); ?>
              </span>
            <?php endif; ?>
          </div>
        </button>

        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-end py-0 blr-ba-notification-menu" aria-labelledby="blr-ba-alerts-toggle">
          <div class="dropdown-menu-header px-3 py-2 border-bottom d-flex align-items-center justify-content-between">
            <span class="fw-semibold"><?php echo esc_html__('Notifications', 'blr-backline'); ?></span>
            <span class="text-muted small">
              <?php
              echo esc_html(
                sprintf(
                  /* translators: %d: unread notification count. */
                  _n('%d unread', '%d unread', $notification_unread_count, 'blr-backline'),
                  $notification_unread_count
                )
              );
              ?>
            </span>
          </div>

          <?php if (empty($notification_items)) : ?>
            <div class="px-3 py-4 text-muted small"><?php echo esc_html__('Nothing needs your attention right now.', 'blr-backline'); ?></div>
          <?php else : ?>
            <div class="list-group list-group-flush">
              <?php foreach ($notification_items as $notification) : ?>
                <?php $is_unread = empty($notification->is_read); ?>
                <a href="<?php echo esc_url(NotificationsModule::link_url($notification)); ?>" class="list-group-item list-group-item-action px-3 py-3<?php echo esc_attr($is_unread ? ' bg-body-tertiary' : ''); ?>">
                  <div class="d-flex align-items-start gap-3">
                    <div class="pt-1">
                      <i class="align-middle text-muted" data-feather="<?php echo esc_attr((string) $notification->icon); ?>"></i>
                    </div>
                    <div class="flex-grow-1 min-w-0">
                      <div class="d-flex align-items-center justify-content-between gap-2 mb-1">
                        <div class="fw-semibold text-truncate"><?php echo esc_html((string) $notification->title); ?></div>
                        <?php if ($is_unread) : ?>
                          <span class="badge bg-primary"><?php echo esc_html__('New', 'blr-backline'); ?></span>
                        <?php endif; ?>
                      </div>
                      <div class="text-muted small mb-1"><?php echo esc_html((string) $notification->message); ?></div>
                      <div class="text-muted small"><?php echo esc_html(NotificationsModule::relative_time((string) ($notification->event_time ?: $notification->updated_at))); ?></div>
                    </div>
                  </div>
                </a>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>

          <div class="dropdown-menu-footer border-top px-3 py-2">
            <a href="<?php echo esc_url(NotificationsModule::page_url()); ?>" class="text-muted text-decoration-none"><?php echo esc_html__('View all notifications', 'blr-backline'); ?></a>
          </div>
        </div>
      </li>
      <?php endif; ?>

      <li class="nav-item dropdown">
          <button
            type="button"
            id="blr-ba-user-menu-toggle"
            class="nav-link dropdown-toggle blr-ba-user-toggle d-flex align-items-center border-0 bg-transparent"
            data-bs-toggle="dropdown"
            data-bs-auto-close="true"
            aria-expanded="false"
          >
          <img class="blr-ba-avatar me-2" src="<?php echo esc_url($avatar_url); ?>" alt="">
          <span class="blr-ba-user-name d-none d-sm-inline">
            <?php echo esc_html($user->display_name ?: $user->user_login); ?>
          </span>
        </button>

        <div class="dropdown-menu dropdown-menu-end" aria-labelledby="blr-ba-user-menu-toggle">
          <a class="dropdown-item" href="<?php echo esc_url($dashboard_base_url . 'profile/'); ?>">
            <i class="align-middle me-1" data-feather="user"></i> <?php echo esc_html__('Profile', 'blr-backline'); ?>
          </a>

          <?php if ($can_view_users) : ?>
            <a class="dropdown-item" href="<?php echo esc_url($dashboard_base_url . 'users/'); ?>">
              <i class="align-middle me-1" data-feather="users"></i> <?php echo esc_html__('Users', 'blr-backline'); ?>
            </a>
          <?php endif; ?>

          <?php if ($can_view_settings) : ?>
            <a class="dropdown-item" href="<?php echo esc_url($dashboard_base_url . 'settings/'); ?>">
              <i class="align-middle me-1" data-feather="settings"></i> <?php echo esc_html__('Settings', 'blr-backline'); ?>
            </a>
          <?php endif; ?>

          <div class="dropdown-divider"></div>

          <a class="dropdown-item" href="<?php echo esc_url(wp_logout_url(home_url('/'))); ?>">
            <i class="align-middle me-1" data-feather="log-out"></i> <?php echo esc_html__('Log out', 'blr-backline'); ?>
          </a>
        </div>
      </li>
    </ul>
  </div>
</nav>
