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

{% block title %}{{ token.name }} — {{ themis_app_name }}{% endblock %}

{% block content %}
<div class="max-w-2xl mx-auto">
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-2xl font-bold">{{ token.name }}</h1>
        <div class="flex gap-2">
            <a href="{% url 'mcp_server:token-edit' pk=token.pk %}" class="btn btn-ghost btn-sm">Edit</a>
            {% if token.is_active %}
            <form method="post" action="{% url 'mcp_server:token-revoke' pk=token.pk %}"
                  onsubmit="return confirm('Revoke this token? It will no longer authenticate MCP requests.');">
                {% csrf_token %}
                <button type="submit" class="btn btn-warning btn-sm btn-outline">Revoke</button>
            </form>
            {% endif %}
            <form method="post" action="{% url 'mcp_server:token-delete' pk=token.pk %}"
                  onsubmit="return confirm('Delete this token permanently? This removes the audit trail.');">
                {% csrf_token %}
                <button type="submit" class="btn btn-error btn-sm btn-outline">Delete</button>
            </form>
        </div>
    </div>

    <div class="card bg-base-200 mb-6">
        <div class="card-body">
            <div class="grid grid-cols-2 gap-y-3">
                <div class="opacity-60">Token ID</div>
                <div class="font-mono">{{ token.get_masked_token }}</div>

                <div class="opacity-60">Status</div>
                <div>
                    {% if token.is_valid %}
                    <span class="badge badge-success badge-sm">Active</span>
                    {% elif not token.is_active %}
                    <span class="badge badge-error badge-sm">Revoked</span>
                    {% else %}
                    <span class="badge badge-error badge-sm">Expired</span>
                    {% endif %}
                </div>

                {% if token.expires_at %}
                <div class="opacity-60">Expires</div>
                <div>{{ token.expires_at }}</div>
                {% endif %}

                <div class="opacity-60">Last Used</div>
                <div>
                    {% if token.last_used_at %}
                    {{ token.last_used_at }} <span class="opacity-60">({{ token.last_used_at|naturaltime }})</span>
                    {% else %}
                    <span class="opacity-60">Never</span>
                    {% endif %}
                </div>

                <div class="opacity-60">Created</div>
                <div>{{ token.created_at }}</div>
            </div>
        </div>
    </div>

    <div class="card bg-base-200 mb-6">
        <div class="card-body">
            <h2 class="card-title text-lg">Allowed Tools</h2>
            {% if token.allowed_tools %}
            <div class="flex flex-wrap gap-2">
                {% for tool in token.allowed_tools %}
                <span class="badge badge-outline font-mono">{{ tool }}</span>
                {% endfor %}
            </div>
            {% else %}
            <p class="opacity-60">All tools permitted.</p>
            {% endif %}
        </div>
    </div>

    <a href="{% url 'mcp_server:token-list' %}" class="btn btn-ghost btn-sm">← Back to Tokens</a>
</div>
{% endblock %}
