{% extends "themis/base.html" %}
{% load humanize %}

{% block title %}API Tokens — {{ themis_app_name }}{% endblock %}

{% block content %}
<div class="max-w-3xl mx-auto">
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-2xl font-bold">API Tokens</h1>
        <a href="{% url 'mcp_server:token-create' %}" class="btn btn-primary btn-sm">Generate Token</a>
    </div>

    <p class="text-sm opacity-60 mb-4">
        Bearer tokens used by MCP clients (Claude Desktop, Cursor, etc.) to call Mnemosyne. Stored hashed at rest — the plaintext is shown only once at creation.
    </p>

    {% if tokens %}
    <div class="space-y-3">
        {% for token in tokens %}
        <div class="card bg-base-200">
            <div class="card-body p-4">
                <div class="flex items-center justify-between">
                    <div>
                        <a href="{% url 'mcp_server:token-detail' pk=token.pk %}"
                           class="font-semibold link link-hover">
                            {{ token.name }}
                        </a>
                        <div class="flex items-center gap-2 mt-1 flex-wrap">
                            <span class="text-sm opacity-60 font-mono">{{ token.get_masked_token }}</span>
                            {% if not token.is_valid %}
                            <span class="badge badge-sm badge-error">
                                {% if not token.is_active %}Revoked{% else %}Expired{% endif %}
                            </span>
                            {% else %}
                            <span class="badge badge-sm badge-success">Active</span>
                            {% endif %}
                            <span class="text-xs opacity-60">
                                Last used:
                                {% if token.last_used_at %}{{ token.last_used_at|naturaltime }}{% else %}never{% endif %}
                            </span>
                        </div>
                    </div>
                    <div class="flex gap-1">
                        <a href="{% url 'mcp_server:token-edit' pk=token.pk %}"
                           class="btn btn-ghost btn-xs">Edit</a>
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="card bg-base-200">
        <div class="card-body items-center text-center py-12">
            <p class="opacity-60 mb-4">No API tokens yet.</p>
            <a href="{% url 'mcp_server:token-create' %}" class="btn btn-primary btn-sm">Generate Your First Token</a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
