{% macro comment_cell(item, depth, parent_author=None) %}
<div class="comment" style="--comment-depth: {{ depth }};" data-depth="{{ depth }}">
    <div class="comment-votes" role="group" aria-label="Comment votes">
        <form method="POST" action="/votes/comment/{{ item.comment['uid'] }}">
            <input type="hidden" name="value" value="1">
            <button type="submit" class="comment-vote-btn vote-up{% if item.my_vote == 1 %} voted{% endif %}" aria-label="Upvote" title="Upvote" aria-pressed="{% if item.my_vote == 1 %}true{% else %}false{% endif %}"{{ guest_disabled(user) }}>+</button>
        </form>
        <span class="comment-vote-count" data-vote-count="{{ item.comment['uid'] }}">{% if user %}{{ item.votes.up - item.votes.down }}{% endif %}</span>
        <form method="POST" action="/votes/comment/{{ item.comment['uid'] }}">
            <input type="hidden" name="value" value="-1">
            <button type="submit" class="comment-vote-btn vote-down{% if item.my_vote == -1 %} voted{% endif %}" aria-label="Downvote" title="Downvote" aria-pressed="{% if item.my_vote == -1 %}true{% else %}false{% endif %}"{{ guest_disabled(user) }}>-</button>
        </form>
    </div>

    <div class="comment-body" id="comment-{{ item.comment['uid'] }}" data-comment-uid="{{ item.comment['uid'] }}">
        <div class="comment-header">
            {% set _user = item.author %}{% set _size = 32 %}{% set _size_class = "sm" %}{% include "_avatar_link.html" %}
            {% set _user = item.author %}{% set _class = "comment-author" %}{% include "_user_link.html" %}
            {% if parent_author %}<span class="comment-reply-to">&#x21B3; @{{ parent_author.get('username', '') }}</span>{% endif %}
            <span class="comment-time">{{ dt_ago(item.comment.get('created_at')) if item.comment.get('created_at') else item.time_ago }}</span>
        </div>
        <div class="comment-text rendered-content" data-raw="{{ item.comment['content'] }}">{{ render_content(item.comment['content'], author_is_admin=is_admin(item.author)) }}</div>
        {% set attachments = item.get('attachments', []) %}
        {% if attachments %}
        {% include "_attachment_display.html" %}
        {% endif %}
        <div class="comment-actions">
            <button type="button" class="comment-action-btn" data-action="reply"{{ guest_disabled(user) }}><span class="icon">💬</span><span class="label"> Reply</span></button>
            {% if owns(item.comment, user) %}
            <button type="button" class="comment-action-btn" data-action="edit" data-edit-url="/comments/edit/{{ item.comment['uid'] }}"><span class="icon">✏️</span><span class="label"> Edit</span></button>
            {% endif %}
            {% if owns(item.comment, user) or is_admin(user) %}
            <form method="POST" action="/comments/delete/{{ item.comment['uid'] }}" class="inline-form comment-delete-form" data-comment-uid="{{ item.comment['uid'] }}">
                <button type="submit" class="comment-action-btn" data-confirm="Delete this comment?"><span class="icon">🗑️</span><span class="label"> Delete</span></button>
            </form>
            {% endif %}
            {% set _type = "comment" %}{% set _uid = item.comment['uid'] %}{% set _reactions = item.reactions %}{% include "_reaction_bar.html" %}
            {% set _type = "comment" %}{% set _uid = item.comment['uid'] %}{% set _owner = item.comment['user_uid'] %}{% set _owner_name = (item.author or {}).get('username', '') %}{% set _class = "comment-action-btn" %}{% include "_report_button.html" %}
        </div>
        {{ caller() }}
    </div>
</div>
{% endmacro %}

{% macro render_comment(item, depth=0) %}
{% set max_depth = 2 %}
{% set visual_depth = depth if depth < max_depth else max_depth %}
{% call comment_cell(item, visual_depth) %}
    {% if item.children %}
    {% if depth < max_depth %}
    <div class="comment-replies">
        {% for child in item.children %}
        {{ render_comment(child, depth + 1) }}
        {% endfor %}
    </div>
    {% else %}
    <div class="comment-replies comment-replies-flat">
        {% for child in item.children %}
        {{ render_comment_flat(child, max_depth, item.author) }}
        {% endfor %}
    </div>
    {% endif %}
    {% endif %}
{% endcall %}
{% endmacro %}

{% macro render_comment_flat(item, depth, parent_author) %}
{% call comment_cell(item, depth, parent_author) %}{% endcall %}
{% for child in item.children %}
{{ render_comment_flat(child, depth, item.author) }}
{% endfor %}
{% endmacro %}
