{% extends "base.html" %}
{% block title %}BinkP Session Transcript — {{ config.BBS_NAME }}{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-file-text"></i> BinkP Session Transcript</h1>
    <p>{{ log.network.name }} — {{ log.started_at|eastern('%Y-%m-%d %H:%M:%S') }}
       {% if log.status == 'success' %}<span style="color:#00ff00;">✓ success</span>
       {% elif log.status == 'error' %}<span style="color:#ff0000;">✗ error</span>
       {% else %}<span style="color:#ffff00;">~ {{ log.status }}</span>{% endif %}
    </p>
</div>

<div class="mb-3">
    <a href="{{ url_for('echomail_admin.logs') }}" class="btn btn-primary btn-sm">
        <i class="bi bi-arrow-left"></i> Poll Logs
    </a>
</div>

{% if log.error_message %}
<div class="alert alert-danger">{{ log.error_message }}</div>
{% endif %}

{% if not log.transcript and log.status == 'running' %}
<div class="alert alert-warning">
    <i class="bi bi-hourglass-split"></i>
    This poll is still in progress and hasn't reached a transcript checkpoint
    yet (checkpoints land at a couple of natural points in the session, not
    every single frame). Refresh this page in a few seconds, or check back
    from the <a href="{{ url_for('echomail_admin.logs') }}">Poll Logs</a> list.
</div>
{% else %}
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>Frame-by-frame session log — <code>&gt;&gt;</code> sent, <code>&lt;&lt;</code> received
            {% if log.status == 'running' %}<span class="badge bg-warning text-dark ms-2">🔄 partial — poll still in progress</span>{% endif %}
        </span>
        <button type="button" class="btn btn-outline-secondary btn-sm" onclick="document.getElementById('transcript-box').scrollTop = 0;">
            <i class="bi bi-arrow-up"></i> Jump to start
        </button>
    </div>
    <div class="card-body">
        <pre id="transcript-box" style="white-space:pre-wrap; font-family:monospace; font-size:0.85rem; max-height:70vh; overflow-y:auto;">{{ log.transcript }}</pre>
    </div>
</div>
{% endif %}
<script>
    // Auto-scroll to the END of the transcript on load, not the start --
    // for a long session (any real file transfer runs to hundreds of
    // frame-by-frame lines) the fixed-height scroll box above defaults
    // to showing the top, so a sysop reading it would see only the
    // handshake and stop right around the first file transfer, never
    // scrolling down to whatever happened at the actual end of the
    // session (the peer's own EOB, our DISCONNECT line, or -- for a
    // failed poll -- the exact frame where things went wrong). That's
    // consistently the most diagnostically useful part, same reasoning
    // _format_transcript() already uses server-side when a transcript
    // is too long to store in full (it keeps the LAST lines, not the
    // first). A "Jump to start" button above lets a sysop who actually
    // wants the handshake get back to it in one click.
    (function () {
        var box = document.getElementById('transcript-box');
        if (box) {
            box.scrollTop = box.scrollHeight;
        }
    })();
</script>
{% endblock %}
