{% extends "base.html" %}

{% block title %}Self-Registration Status — Admin{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-broadcast"></i> Federation Self-Registration</h1>
    <p class="text-muted mb-0">Status of THIS BBS's registration against the upstream federation hub.</p>
</div>

<div class="row g-3">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><i class="bi bi-gear"></i> Configuration</div>
            <div class="card-body">
                <dl class="mb-0">
                    <dt>Hub URL</dt>
                    <dd><code>{{ hub or '(not set)' }}</code></dd>
                    <dt>Self-register enabled</dt>
                    <dd>
                        {% if enabled %}
                            <span class="badge bg-success">on</span>
                        {% else %}
                            <span class="badge bg-secondary">off</span> — set <code>REGISTRY_SELF_REGISTER=true</code>
                        {% endif %}
                    </dd>
                    <dt>Our host (BBS_DOMAIN)</dt>
                    <dd><code>{{ meta.host or '(blank)' }}</code></dd>
                    <dt>Our name (BBS_NAME)</dt>
                    <dd>{{ meta.name }}</dd>
                    <dt>Sysop</dt>
                    <dd>{{ meta.sysop or '(blank)' }}</dd>
                    <dt>Location</dt>
                    <dd>{{ meta.location or '(blank)' }}</dd>
                    <dt>Contact email</dt>
                    <dd>{{ meta.contact_email or '(blank — required!)' }}</dd>
                    <dt>Software / version</dt>
                    <dd>{{ meta.software }} {{ meta.software_version }}</dd>
                    <dt>MSP / SYSTAT ports</dt>
                    <dd>{{ meta.msp_port }} / {{ meta.systat_port }}</dd>
                </dl>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><i class="bi bi-arrow-clockwise"></i> Last Hub Response</div>
            <div class="card-body">
                {% if not state %}
                    <p class="text-muted">We have not yet contacted the hub. The daemon registers automatically on next tick; click below to force it now.</p>
                {% else %}
                    {% if state.last_register_at %}
                        <p class="small mb-1"><strong>Last register:</strong> <code>{{ state.last_register_at }}</code></p>
                    {% endif %}
                    {% if state.last_heartbeat_at %}
                        <p class="small mb-1"><strong>Last heartbeat:</strong> <code>{{ state.last_heartbeat_at }}</code></p>
                    {% endif %}
                    {% set resp = state.last_response or {} %}
                    {% if resp.status %}
                        <p><strong>Hub status:</strong>
                            {% if resp.status == 'listed' %}
                                <span class="badge bg-success">listed ✓</span>
                            {% elif resp.status == 'pending_verification' %}
                                <span class="badge bg-warning text-dark">awaiting email verify</span>
                            {% elif resp.status == 'pending_approval' %}
                                <span class="badge bg-info text-dark">awaiting hub sysop approval</span>
                            {% else %}
                                <span class="badge bg-secondary">{{ resp.status }}</span>
                            {% endif %}
                        </p>
                    {% endif %}
                    {% if state.verify_url %}
                        <div class="alert alert-warning small">
                            <strong>Action required:</strong> click this verify link to confirm your contact email — until you do, your BBS won't appear in <code>anetbbs.lst</code>.
                            <br>
                            <a href="{{ state.verify_url }}" target="_blank" rel="noopener" style="word-break:break-all;">{{ state.verify_url }}</a>
                        </div>
                    {% endif %}
                    {% if resp.message %}
                        <p class="small text-muted">{{ resp.message }}</p>
                    {% endif %}
                {% endif %}
            </div>
        </div>
    </div>
</div>

<div class="mt-3">
    <form method="post" action="{{ url_for('admin.registry_self_register_now') }}" class="d-inline">
        {% if csrf_token is defined %}<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">{% endif %}
        <button class="btn btn-primary"><i class="bi bi-arrow-clockwise"></i> Register / Heartbeat Now</button>
    </form>
    <a href="{{ url_for('admin.registry_index') }}" class="btn btn-outline-secondary">← Back to Registry</a>
</div>

{% endblock %}
