{% extends 'base.html' %}

{% block title %}API Tokens - Packs{% endblock %}

{% block content %}
    <h1>API Tokens</h1>
    <p><a href="{% url 'profiles:account' %}"><i class="fas fa-user-cog"></i> Account Settings</a></p>

    {% if messages %}
        <ul class="messages">
            {% for message in messages %}
                <li>{{ message }}</li>
            {% endfor %}
        </ul>
    {% endif %}

    <h2>Tokens</h2>
    <p>Tokens let non-browser clients (like a Fabric mod) access the API and download packs.</p>
    <p class="token-hint">
        Send credentials as <code>Authorization: Bearer &lt;username&gt; &lt;token&gt;</code>,
        e.g. <code>Authorization: Bearer {{ request.user.username }} &lt;token&gt;</code>
    </p>
    <p><a href="{% url 'api:docs' %}" class="btn btn-secondary btn-sm"><i class="fas fa-book"></i> Interactive API Docs</a></p>

    <form method="post" action="{% url 'profiles:token_create' %}" class="token-create">
        {% csrf_token %}
        <input type="text" name="label" placeholder="Token label (e.g. Fabric updater)" required>
        <button type="submit">Create token</button>
    </form>

    {% if tokens %}
        <table class="token-table">
            <thead>
                <tr>
                    <th>Label</th>
                    <th>Credential</th>
                    <th>Created</th>
                    <th>Last used</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for token in tokens %}
                    <tr>
                        <td>{{ token.label }}</td>
                        <td><code>{{ request.user.username }} {{ token.token }}</code></td>
                        <td>{{ token.created_at }}</td>
                        <td>{{ token.last_used|default:"never" }}</td>
                        <td>
                            <form method="post" action="{% url 'profiles:token_delete' token.id %}">
                                {% csrf_token %}
                                <button type="submit">Revoke</button>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>No tokens yet.</p>
    {% endif %}
{% endblock %}