{% extends "base.html" %}
{% block title %}Inter-BBS IM — {{ config.BBS_NAME }}{% endblock %}

{% block content %}
<div class="container my-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h2 class="mb-1"><i class="bi bi-chat-dots-fill"></i> Inter-BBS Instant Messages</h2>
            <p class="text-muted mb-0">
                Real-time messages received from other BBSes via MSP (RFC 1312).
            </p>
        </div>
        <div>
            <a href="{{ url_for('imsg.directory') }}" class="btn btn-outline-primary">
                <i class="bi bi-globe2"></i> BBS Directory
            </a>
            <a href="{{ url_for('imsg.send') }}" class="btn btn-success">
                <i class="bi bi-send"></i> Send IM
            </a>
        </div>
    </div>

    {% if not messages %}
        <div class="alert alert-info">
            No instant messages yet. Other Synchronet/anetbbs sysops can ping
            you on TCP port 18 — make sure the port is reachable.
        </div>
    {% else %}
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>From</th>
                    <th>Host</th>
                    <th>Message</th>
                    <th>Received</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for m in messages %}
                <tr class="{% if not m.is_read %}table-warning{% endif %}">
                    <td><strong>{{ m.sender_label or '(anon)' }}</strong></td>
                    <td><code>{{ m.sender_host }}</code></td>
                    <td><pre style="margin:0; white-space:pre-wrap; max-width:600px;">{{ m.body }}</pre></td>
                    <td>{{ m.received_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td class="text-nowrap">
                        {# Reply: pre-fill the send form with the original sender + host. #}
                        {% set reply_to = (m.sender_label or '').split('@')[0] %}
                        <a class="btn btn-outline-primary btn-sm"
                           href="{{ url_for('imsg.send', recipient=reply_to, host=m.sender_host) }}"
                           title="Reply">
                            <i class="bi bi-reply"></i>
                        </a>
                        <form method="post"
                              action="{{ url_for('imsg.delete', msg_id=m.id) }}"
                              class="d-inline"
                              onsubmit="return confirm('Delete this IM?');">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button class="btn btn-outline-danger btn-sm">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}
</div>
{% endblock %}
