{% extends "dashboard/base.html" %}
{% set active_page = 'tokens' %}

{% block title %}API Tokens - SmartTools Dashboard{% endblock %}

{% block dashboard_header %}
<div class="flex items-center justify-between">
    <div>
        <h1 class="text-2xl font-bold text-gray-900">API Tokens</h1>
        <p class="mt-1 text-gray-600">Manage tokens for CLI and API access</p>
    </div>
    <button type="button"
            onclick="openCreateTokenModal()"
            class="inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-md hover:bg-indigo-700">
        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
        </svg>
        Create Token
    </button>
</div>
{% endblock %}

{% block dashboard_content %}
<!-- Info Banner -->
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
    <div class="flex items-start">
        <svg class="w-5 h-5 text-blue-500 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
        </svg>
        <div class="ml-3">
            <h4 class="text-sm font-medium text-blue-800">About API Tokens</h4>
            <p class="mt-1 text-sm text-blue-700">
                API tokens are used to authenticate with the SmartTools registry from the CLI.
                Use <code class="px-1 bg-blue-100 rounded">smarttools auth login</code> to authenticate,
                or set the <code class="px-1 bg-blue-100 rounded">SMARTTOOLS_TOKEN</code> environment variable.
            </p>
        </div>
    </div>
</div>

{% if tokens %}
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Name
                </th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Created
                </th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Last Used
                </th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Status
                </th>
                <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Actions
                </th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            {% for token in tokens %}
            <tr class="hover:bg-gray-50">
                <td class="px-6 py-4 whitespace-nowrap">
                    <div class="flex items-center">
                        <div class="w-10 h-10 bg-cyan-100 rounded-lg flex items-center justify-center flex-shrink-0">
                            <svg class="w-5 h-5 text-cyan-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"/>
                            </svg>
                        </div>
                        <div class="ml-4">
                            <p class="text-sm font-medium text-gray-900">{{ token.name }}</p>
                            <p class="text-xs text-gray-500 font-mono">st_...{{ token.token_suffix }}</p>
                        </div>
                    </div>
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ token.created_at|date_format }}
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ token.last_used_at|timeago if token.last_used_at else 'Never' }}
                </td>
                <td class="px-6 py-4 whitespace-nowrap">
                    {% if token.revoked_at %}
                    <span class="px-2 py-1 text-xs font-medium text-red-800 bg-red-100 rounded-full">
                        Revoked
                    </span>
                    {% else %}
                    <span class="px-2 py-1 text-xs font-medium text-green-800 bg-green-100 rounded-full">
                        Active
                    </span>
                    {% endif %}
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                    {% if not token.revoked_at %}
                    <button type="button"
                            onclick="revokeToken({{ token.id }}, '{{ token.name }}')"
                            class="text-red-600 hover:text-red-900">
                        Revoke
                    </button>
                    {% else %}
                    <span class="text-gray-400">Revoked {{ token.revoked_at|timeago }}</span>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% else %}
<!-- Empty State -->
<div class="bg-white rounded-lg border border-gray-200 text-center py-16">
    <svg class="mx-auto w-16 h-16 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"/>
    </svg>
    <h3 class="mt-4 text-lg font-medium text-gray-900">No API tokens</h3>
    <p class="mt-2 text-gray-600 max-w-sm mx-auto">
        Create an API token to authenticate with the registry from the command line.
    </p>
    <button type="button"
            onclick="openCreateTokenModal()"
            class="mt-6 inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-md hover:bg-indigo-700">
        Create Your First Token
    </button>
</div>
{% endif %}

<!-- Create Token Modal -->
<div id="create-token-modal" class="hidden fixed inset-0 z-50 overflow-y-auto" aria-modal="true" role="dialog">
    <div class="min-h-screen px-4 text-center">
        <div class="fixed inset-0 bg-black bg-opacity-50" onclick="closeCreateTokenModal()"></div>
        <div class="inline-block w-full max-w-md my-8 text-left align-middle bg-white shadow-xl rounded-lg">
            <form id="create-token-form" onsubmit="createToken(event)">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

                <div class="p-6">
                    <h3 class="text-lg font-semibold text-gray-900">Create API Token</h3>
                    <p class="mt-2 text-sm text-gray-600">
                        Give your token a descriptive name so you can identify it later.
                    </p>

                    <div class="mt-4">
                        <label for="token-name" class="block text-sm font-medium text-gray-700 mb-1">
                            Token name
                        </label>
                        <input type="text"
                               name="name"
                               id="token-name"
                               required
                               placeholder="e.g., Laptop CLI, CI/CD Pipeline"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500">
                    </div>
                </div>

                <div class="bg-gray-50 px-6 py-4 rounded-b-lg flex justify-end gap-3">
                    <button type="button"
                            onclick="closeCreateTokenModal()"
                            class="px-4 py-2 text-sm font-medium text-gray-700 hover:text-gray-900">
                        Cancel
                    </button>
                    <button type="submit"
                            class="px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-md hover:bg-indigo-700">
                        Create Token
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Token Created Modal (shows the token once) -->
<div id="token-created-modal" class="hidden fixed inset-0 z-50 overflow-y-auto" aria-modal="true" role="dialog">
    <div class="min-h-screen px-4 text-center">
        <div class="fixed inset-0 bg-black bg-opacity-50"></div>
        <div class="inline-block w-full max-w-lg my-8 text-left align-middle bg-white shadow-xl rounded-lg">
            <div class="p-6">
                <div class="flex items-center mb-4">
                    <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                        <svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                        </svg>
                    </div>
                    <h3 class="ml-3 text-lg font-semibold text-gray-900">Token Created</h3>
                </div>

                <div class="bg-amber-50 border border-amber-200 rounded-lg p-4 mb-4">
                    <div class="flex items-start">
                        <svg class="w-5 h-5 text-amber-500 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
                        </svg>
                        <p class="ml-3 text-sm text-amber-800">
                            <strong>Copy your token now.</strong> You won't be able to see it again!
                        </p>
                    </div>
                </div>

                <div class="bg-gray-100 rounded-lg p-4">
                    <div class="flex items-center justify-between">
                        <code id="new-token-value" class="text-sm font-mono text-gray-800 break-all"></code>
                        <button type="button"
                                onclick="copyNewToken()"
                                class="ml-4 p-2 text-gray-400 hover:text-gray-600 flex-shrink-0">
                            <svg id="copy-token-icon" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
                            </svg>
                            <svg id="check-token-icon" class="w-5 h-5 hidden text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                            </svg>
                        </button>
                    </div>
                </div>

                <p class="mt-4 text-sm text-gray-600">
                    To use this token, run:
                </p>
                <pre class="mt-2 bg-gray-900 text-gray-100 text-sm p-3 rounded-lg overflow-x-auto"><code>export SMARTTOOLS_TOKEN="<span id="token-in-export"></span>"</code></pre>
            </div>

            <div class="bg-gray-50 px-6 py-4 rounded-b-lg flex justify-end">
                <button type="button"
                        onclick="closeTokenCreatedModal()"
                        class="px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-md hover:bg-indigo-700">
                    Done
                </button>
            </div>
        </div>
    </div>
</div>

<script>
function openCreateTokenModal() {
    document.getElementById('create-token-modal').classList.remove('hidden');
    document.getElementById('token-name').focus();
}

function closeCreateTokenModal() {
    document.getElementById('create-token-modal').classList.add('hidden');
    document.getElementById('token-name').value = '';
}

async function createToken(event) {
    event.preventDefault();
    const form = event.target;
    const name = form.querySelector('[name="name"]').value;

    try {
        const response = await fetch('/api/v1/tokens', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({name: name})
        });

        if (response.ok) {
            const data = await response.json();
            closeCreateTokenModal();
            showNewToken(data.token);
        } else {
            const error = await response.json();
            alert(error.error || 'Failed to create token');
        }
    } catch (err) {
        alert('Failed to create token. Please try again.');
    }
}

function showNewToken(token) {
    document.getElementById('new-token-value').textContent = token;
    document.getElementById('token-in-export').textContent = token;
    document.getElementById('token-created-modal').classList.remove('hidden');
}

function closeTokenCreatedModal() {
    document.getElementById('token-created-modal').classList.add('hidden');
    window.location.reload();
}

function copyNewToken() {
    const token = document.getElementById('new-token-value').textContent;
    navigator.clipboard.writeText(token).then(() => {
        document.getElementById('copy-token-icon').classList.add('hidden');
        document.getElementById('check-token-icon').classList.remove('hidden');
        setTimeout(() => {
            document.getElementById('copy-token-icon').classList.remove('hidden');
            document.getElementById('check-token-icon').classList.add('hidden');
        }, 2000);
    });
}

async function revokeToken(tokenId, tokenName) {
    if (!confirm(`Are you sure you want to revoke "${tokenName}"? This cannot be undone.`)) {
        return;
    }

    try {
        const response = await fetch(`/api/v1/tokens/${tokenId}`, {
            method: 'DELETE'
        });

        if (response.ok) {
            window.location.reload();
        } else {
            const error = await response.json();
            alert(error.error || 'Failed to revoke token');
        }
    } catch (err) {
        alert('Failed to revoke token. Please try again.');
    }
}
</script>
{% endblock %}
