{% extends "core/base.html" %}
{% load lyric_extras %}

{% block title_text %}Billpost{% endblock title_text %}
{% block header_text %}<span>Bill</span><span>post</span>{% endblock header_text %}


{% block content %}
{# Hidden owner span — preserves the existing FT contract that reads the    #}
{# post owner via #id_post_owner. Visible owner attribution moved into the  #}
{# self line ("just me, …" / "& me, …") below.                              #}
<span id="id_post_owner" hidden>{{ post.owner|display_name }}</span>

<div class="post-page">
    {% if viewer_is_owner %}
        {# Owner viewing — owner-centric "shared between … & me" prose, via the #}
        {# shared header (also used by the room game-views POST chat).          #}
        {% include "apps/billboard/_partials/_post_header.html" with header_title=post.title other_recipients=other_recipients self_user=post.owner %}
    {% else %}
        {# Invitee viewing — "shared with" prose centred on the viewer          #}
        {# (request.user). Sole invitee collapses to a single line; the         #}
        {# "created by …" line attributes the post to its founder. Bespoke      #}
        {# (the shared header is owner-style), but reuses the chip partial.      #}
        <header class="post-header">
            <h3 class="post-title">{{ post.title }}</h3>
            {% if other_recipients %}
                <p class="post-shared-recipients">shared with {% for r in other_recipients %}{% include "apps/billboard/_partials/_post_recipient.html" with r=r %}{% if not forloop.last %}, {% endif %}{% endfor %}</p>
                <p class="post-shared-self">&amp; me, <span class="post-attribution">{{ request.user|at_handle }} the {{ request.user.active_title_display }}</span></p>
            {% else %}
                <p class="post-shared-self">shared with me, <span class="post-attribution">{{ request.user|at_handle }} the {{ request.user.active_title_display }}</span></p>
            {% endif %}
            <p class="post-created-by">created by <span class="post-attribution">{{ post.owner|at_handle }} the {{ post.owner.active_title_display }}</span></p>
        </header>
    {% endif %}

    {# @mailman invite Lines have no in-line OK/BYE block — the Line's prose  #}
    {# embeds a post-attribution anchor (see apps.billboard.mail.            #}
    {# INVITE_TEMPLATE) routing to the owner's bud page, where clicking the  #}
    {# glowing sea sub-btn accepts the invite on visit. No explicit accept/  #}
    {# decline surface. Per-line markup lives in _post_line.html (shared w.  #}
    {# the room game-views carousel POST view + epic:room_post AJAX append). #}
    <ul id="id_post_table" class="post-lines">
        {% for line in post.lines.all %}
            {% include "apps/billboard/_partials/_post_line.html" %}
        {% endfor %}
        <li class="post-line-buffer" aria-hidden="true"></li>
    </ul>

    {# System-author Posts (note unlocks, tax ledger) — input is read-only:#}
    {# the user can't respond, and the placeholder calls that out. View_   #}
    {# post hard-rejects POSTs to these kinds; the post_save Line signal   #}
    {# is the safety net for ORM-level / API writes that bypass the view.  #}
    {% if post.kind == 'note_unlock' or post.kind == 'tax_ledger' or post.kind == 'mail_acceptance' %}
        <form id="id_post_line_form" class="post-line-form">
            <input
                id="id_post_line_text"
                name="text"
                class="form-control"
                placeholder="No response needed at this time"
                readonly
            />
        </form>
    {% else %}
        <form id="id_post_line_form" method="POST" action="{% url 'billboard:view_post' post.id %}" class="post-line-form">
            {% csrf_token %}
            <div class="composer-row">
                <input
                    id="id_post_line_text"
                    name="text"
                    class="form-control{% if form.errors.text %} is-invalid{% endif %}"
                    placeholder="Enter a post line"
                    value="{{ form.text.value|default:'' }}"
                    aria-describedby="id_post_line_feedback"
                    required
                />
                <button type="submit" id="id_post_line_btn" class="btn btn-confirm">OK</button>
            </div>
            {% if form.errors %}
                <div id="id_post_line_feedback" class="invalid-feedback">
                    {{ form.errors.text.0 }}
                </div>
            {% endif %}
        </form>
    {% endif %}

    {# Bud btn (bottom-left) + slide-out recipient field — async share.  #}
    {# Suppressed on system-author Posts (note unlock + tax ledger threads) #}
    {# since friend-invites don't apply to system-authored threads.        #}
    {% if post.kind != 'note_unlock' and post.kind != 'tax_ledger' and post.kind != 'mail_acceptance' %}
        {% include "apps/billboard/_partials/_bud_panel.html" %}
    {% endif %}

    {# Gear btn (bottom-right) + menu — NVM always; DEL (owner) / BYE     #}
    {# (invitee) gated to user-Posts.                                     #}
    {% include "apps/billboard/_partials/_post_gear.html" %}
</div>
{% endblock content %}

{% block scripts %}
    {% include "apps/dashboard/_partials/_scripts.html" %}
{% endblock scripts %}
