{% from "_macros.html" import icon, brandlink, model_avatar %}
{#
  Sidebar: brand, new chat, pinned models, the folder tree, then unfiled chats.

  "New chat" is a link, not a button that creates a row. The chat is written
  when the first message is sent, so opening one and walking away leaves
  nothing behind.
#}
<aside class="sidebar" id="sidebar">
  <div class="sidebar__header">
    {{ brandlink(uid="side") }}
  </div>

  {% include "partials/_sidebar_actions.html" %}

  {# Every 10s, refresh the unread dots and announce anything that finished
     while this page was showing something else. Out-of-band spans only, so the
     folder tree keeps its open/closed state. #}
  <div hidden hx-get="/api/chats/unread" hx-trigger="every 10s"
       hx-swap="none"></div>

  <nav class="sidebar__scroll" aria-label="Chats">
    {% include "partials/_sidebar_pinned.html" %}
    {% include "partials/_sidebar_sections.html" %}
    {% include "partials/_sidebar_tree.html" %}
  </nav>

  <div class="sidebar__footer">
    {% if can.get("library.use") %}
    <a class="nav-item" href="/library/knowledge">
      {{ icon("archive", "icon--sm") }}
      <span class="nav-item__label">Library</span>
    </a>
    {% endif %}

    {% if can.get("agent.ssh") %}
    <a class="nav-item" href="/agents">
      {{ icon("server", "icon--sm") }}
      <span class="nav-item__label">Connections</span>
    </a>
    {% endif %}

    <a class="nav-item" href="/settings">
      {{ icon("user", "icon--sm") }}
      <span class="nav-item__label">{{ user.name }}</span>
      {% if user.is_admin %}<span class="badge badge--leaf">admin</span>{% endif %}
    </a>

    <div class="sidebar__tools">
      {% if user.is_admin %}
      <a class="btn btn--icon" href="/admin" aria-label="Admin settings" title="Admin settings">
        {{ icon("shield") }}
      </a>
      {% endif %}
      <button class="btn btn--icon" type="button" data-theme-toggle
              aria-label="Switch theme" title="Switch theme">
        <span class="theme-icon theme-icon--dark">{{ icon("moon") }}</span>
        <span class="theme-icon theme-icon--light">{{ icon("sun") }}</span>
      </button>
      <span class="spacer"></span>
      <form method="post" action="/auth/logout">
        <button class="btn btn--icon" type="submit" aria-label="Sign out" title="Sign out">
          {{ icon("logout") }}
        </button>
      </form>
    </div>
  </div>
</aside>
