{% extends 'base.html.twig' %}
{% from '_macros/icon.html.twig' import icon %}

{% block site_nav_active app.user is same as(user) ? 'user' %}
{% block user_toolbox_active app.request.attributes.get('_route') %}

{% block sidebar %}
  {% from '_macros/time.html.twig' import relative_time %}

  <section class="sidebar__section flow break-text">
    <header>
      <h1 class="sidebar__title">
        <a href="{{ path('user', {username: user.username}) }}">{{ user.username }}</a>
        {% if user.admin %}
          <span title="{{ 'user_flag.admin'|trans }}">
          {{ icon('wrench', 'user_flag.admin'|trans, '', 'text-sm') }}
        </span>
        {% endif %}
      </h1>

      <p>
        <small class="fg-muted text-sm">{{ 'user.registered'|trans({
            '%timestamp%': relative_time(user.created, { time_format: 'none', natural: true })
          })|raw }}
        </small>
      </p>

      {% if user.pronouns is not empty %}
        <p>
          <small class="fg-muted text-sm">
            <strong>{{- 'label.pronouns'|trans }}:</strong> {{ user.pronouns -}}
          </small>
        </p>
      {% endif %}

      {% if user is same as(app.user) or is_granted('ROLE_ADMIN') %}
        <p>
          <small class="fg-muted text-sm">
            {%- if user.allowlisted -%}
              {{ 'label.allowlisted'|trans -}}
            {%- else -%}
              {{ 'label.not_allowlisted'|trans -}}
            {%- endif -%}
          </small>
        </p>
      {% endif %}
    </header>

    {% if user.biography is not empty %}
      <div class="user-bio__biography text-flow" hx-disable>
        {{- user.biography|markdown|raw -}}
      </div>
    {% endif %}

    {% if user is same as(app.user) and is_granted('edit_biography', user) %}
      <p>
        <a href="{{ path('edit_biography', { username: user.username }) }}"
           class="button button--flex button--small button--secondary">
          {{ icon('pencil') }}
          <span>{{ 'nav.edit_biography'|trans }}</span>
        </a>
      </p>
    {% endif %}

    {% if is_granted('ROLE_USER') and app.user.blocking(user) %}
      <form action="{{ path('unblock_user', { username: user.username }) }}" method="POST">
        <input type="hidden" name="_token" value="{{ csrf_token('unblock') }}">

        <p>
          <button class="button button--secondary">
            {{- 'action.unblock'|trans -}}
          </button>
        </p>
      </form>
    {% endif  %}
  </section>

  {% set active_ban = user.activeBan %}

  {% if is_granted('ROLE_ADMIN') and active_ban %}
    <section class="sidebar__section flow bg-red">
      <h1 class="sidebar__title">{{ 'heading.this_user_is_banned'|trans }}</h1>

      <dl class="definition-list">
        <dt>{{ 'label.reason'|trans }}</dt>
        <dd>{{ active_ban.reason }}</dd>

        <dt>{{ 'label.banned_by'|trans }}</dt>
        <dd>
          <a href="{{ path('user', { username: active_ban.bannedBy.username }) }}">
            {{- active_ban.bannedBy.username -}}
          </a>
        </dd>

        <dt>{{ 'label.expires'|trans }}</dt>
        <dd>
          {%- if active_ban.expires -%}
            {{ relative_time(active_ban.expires) }}
          {%- else -%}
            <small class="fg-muted text-md">{{ 'label.never'|trans }}</small>
          {%- endif -%}
        </dd>
      </dl>

      <p>
        <a href="{{ path('unban_user', { username: user.username }) }}"
           class="button">
          {{- 'action.unban'|trans -}}
        </a>
      </p>
    </section>
  {% endif %}

  {% set toolbox_items = {
    compose_message: {
      label: 'user.message'|trans,
      path: path('compose_message', { username: user.username }),
      condition: is_granted('ROLE_USER') and user is not same as(app.user) and is_granted('message', user),
    },
    block_user: {
      label: 'nav.block_user'|trans,
      path: path('block_user', { username: user.username }),
      condition: is_granted('ROLE_USER') and user is not same as(app.user) and not app.user.isBlocking(user),
    },
    ban_user: {
      label: 'action.ban'|trans,
      path: path('ban_user', { username: user.username }),
      condition: is_granted('ROLE_ADMIN') and user is not same as(app.user) and not active_ban,
    },
    user_forum_bans: {
      label: 'nav.forum_bans'|trans,
      path: path('user_forum_bans', { username: user.username }),
      condition: is_granted('ROLE_ADMIN')
    },
    edit_biography: {
      label: 'nav.edit_biography'|trans,
      path: path('edit_biography', { username: user.username }),
      condition: is_granted('edit_biography', user) and user is not same as(app.user)
    },
    hidden_forums: {
      label: 'nav.hidden_forums'|trans,
      path: path('hidden_forums', { username: user.username }),
      condition: is_granted('ROLE_USER') and user is same as(app.user)
    },
    user_trash: {
      label: 'nav.trash'|trans,
      path: path('user_trash', { username: user.username }),
      condition: is_granted('ROLE_ADMIN') or user is same as(app.user)
    },
    delete_account: {
      label: 'action.delete_account'|trans,
      path: path('delete_account', { username: user.username }),
      condition: is_granted('ROLE_ADMIN'),
    },
  }|filter(item => item.condition)|sort((a, b) => a.label <=> b.label) %}

  {% if toolbox_items is not empty %}
    <section class="sidebar__section flow">
      <h1 class="sidebar__title">{{ 'label.toolbox'|trans }}</h1>
      <ul class="unlistify sidebar__no-padding">
        {% for key, item in toolbox_items %}
          <li>
            <a href="{{ item.path }}"
               class="menu-item {{ key == block('user_toolbox_active') ? 'menu-item--active' }}">
              {{- item.label -}}
            </a>
          </li>
        {% endfor %}
      </ul>
    </section>
  {% endif %}

  {% if is_granted('ROLE_ADMIN') %}
    <section class="sidebar__section flow">
      <h1 class="sidebar__title">{{ 'heading.allowlisting'|trans }}</h1>

      <p class="fg-muted">{{ 'help.allowlisting'|trans }}</p>

      <form action="{{ path(user.allowlisted ? 'user_deallowlist' : 'user_allowlist', {username: user.username}) }}" method="post" class="form">
        <input type="hidden" name="_token" value="{{ csrf_token('allowlist') }}">
        <div class="form-flex form__row">
          <button class="button button--secondary">
            {{ not user.allowlisted ? 'action.allowlist'|trans : 'action.allowlist_remove'|trans }}
          </button>
        </div>
      </form>
    </section>
  {% endif %}

  {% if user.moderatorTokens|length > 0 %}
    <section class="sidebar__section flow">
      <h1 class="sidebar__title break-text">{{ 'user.moderates'|trans({'%username%': user.username}) }}</h1>

      <ul class="tag-cloud sidebar__tag-cloud">
        {% for token in user.moderatorTokens %}
          <li>
            <a href="{{ path('forum', {forum_name: token.forum.name}) }}" class="tag">
              {{- token.forum.name -}}
            </a>
          </li>
        {% endfor %}
      </ul>
    </section>
  {% endif %}
{% endblock %}
