{% load i18n %}{% load anyllm_tags %}
{% show_internal as show_internal %}

{% if message.role == "tool" and not show_internal %}
  {# Skip tool messages in production #}
{% elif message.role == "assistant" and not message.content and not show_internal %}
  {# Skip empty assistant messages when internal details are hidden #}
{% elif message.role == "assistant" and not message.content and show_internal and not message.reasoning and not message.tool_calls %}
  {# Skip empty assistant messages even in debug if there's nothing to show #}
{% else %}
<div class="anyllm-message-wrapper {{ message.role }}" id="msg-{{ message.pk }}">
  <div class="anyllm-avatar {{ message.role }}">
    {% if message.role == "user" %}{% trans "U" %}{% elif message.role == "assistant" %}{% trans "AI" %}{% else %}{% trans "T" %}{% endif %}
  </div>
  <div class="anyllm-message-body">
    <div class="anyllm-message-meta">
      <span class="anyllm-message-role">
        {% if message.role == "user" %}{% trans "You" %}{% elif message.role == "assistant" %}{% trans "Assistant" %}{% else %}{% trans "Tool" %}{% endif %}
      </span>
      <span class="anyllm-message-time">{{ message.created_at|date:"g:i A" }}</span>
    </div>

    {% if show_internal and message.reasoning %}
      <div class="anyllm-reasoning">
        <div class="anyllm-reasoning-label">{% trans "Reasoning" %}</div>
        <div class="anyllm-reasoning-content">{{ message.reasoning }}</div>
      </div>
    {% endif %}

    <div class="anyllm-message {{ message.role }}">
      {% if message.role == "tool" %}
        <div class="anyllm-tool-call" style="margin-bottom: 0.375rem;">
          <span>🔧</span>
          <span>{% blocktrans with name=message.tool_results.name|default:"tool" %}Result from <strong>{{ name }}</strong>{% endblocktrans %}</span>
        </div>
        <div class="anyllm-tool-result">{{ message.content }}</div>
      {% else %}
        <div class="content">{{ message.content|anyllm_markdown }}</div>
        {% if show_internal and message.tool_calls %}
          {% for tc in message.tool_calls %}
            <div class="anyllm-tool-call">
              <span>🔧</span>
              <span>{% blocktrans with name=tc.function.name %}Called <strong>{{ name }}</strong>{% endblocktrans %}</span>
            </div>
          {% endfor %}
        {% endif %}
      {% endif %}
    </div>

    {% if message.files.exists %}
      <div class="anyllm-files">
        {% for f in message.files.all %}
          {% if f.mime_type|slice:":6" == "image/" %}
            <img src="{{ f.file.url }}" alt="">
          {% else %}
            <a href="{{ f.file.url }}" target="_blank" class="anyllm-file-link">
              <span>📎</span>
              <span>{{ f.file.name }}</span>
            </a>
          {% endif %}
        {% endfor %}
      </div>
    {% endif %}
  </div>
</div>
{% endif %}
