<?php
/** @var \BotGuard\Core\View $this @var \BotGuard\Core\Csrf $csrf @var array $keys @var string|null $newKey */
$this->layout('layouts.app');
$appUrl = e(rtrim((string) config('app.url', ''), '/'));
?>
<h1 class="h4 mb-3">API Keys</h1>

<?php if (! empty($newKey)): ?>
    <div class="alert alert-success">
        <div class="fw-semibold mb-1"><i class="bi bi-check-circle"></i> New API key created — copy it now</div>
        <code class="user-select-all d-block p-2 bg-body rounded border"><?= e($newKey) ?></code>
        <div class="small mt-1">This is the only time the full key is shown.</div>
    </div>
<?php endif; ?>

<div class="row g-4">
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table align-middle mb-0">
                        <thead><tr class="text-secondary small text-uppercase"><th>Name</th><th>Key</th><th>Last used</th><th></th></tr></thead>
                        <tbody>
                            <?php if (empty($keys)): ?>
                                <tr><td colspan="4" class="text-center text-secondary py-4">No API keys yet.</td></tr>
                            <?php else: foreach ($keys as $k): ?>
                                <tr class="<?= $k['revoked'] ? 'opacity-50' : '' ?>">
                                    <td><?= e($k['name']) ?></td>
                                    <td><code><?= e($k['key_prefix']) ?>…</code></td>
                                    <td class="small text-secondary"><?= e($k['last_used_at'] ?? 'never') ?></td>
                                    <td class="text-end">
                                        <?php if ($k['revoked']): ?>
                                            <span class="badge text-bg-secondary">revoked</span>
                                        <?php else: ?>
                                            <form method="post" action="/dashboard/api-keys/revoke" class="d-inline"
                                                  onsubmit="return confirm('Revoke this key? Applications using it will stop working.');">
                                                <?= $csrf->field() ?>
                                                <input type="hidden" name="id" value="<?= e($k['id']) ?>">
                                                <button class="btn btn-outline-danger btn-sm" type="submit">Revoke</button>
                                            </form>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="card-footer bg-body">
                <form method="post" action="/dashboard/api-keys" class="row g-2">
                    <?= $csrf->field() ?>
                    <div class="col-sm-8"><input type="text" name="name" class="form-control" placeholder="Key name (e.g. Production server)"></div>
                    <div class="col-sm-4"><button class="btn btn-primary w-100" type="submit"><i class="bi bi-plus-lg"></i> Generate key</button></div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card border-0 bg-body-tertiary">
            <div class="card-body">
                <h6><i class="bi bi-code-slash text-primary"></i> Using the API</h6>
                <p class="small text-secondary mb-2">Authenticate with a Bearer token:</p>
<pre class="small bg-body p-2 rounded border mb-2"><code>curl <?= $appUrl ?>/api/v1/stats \
  -H "Authorization: Bearer &lt;key&gt;"</code></pre>
                <p class="small text-secondary mb-0">Endpoints: <code>GET /api/v1/stats</code>,
                    <code>GET /api/v1/results?limit=50</code>. Data is scoped to your account.</p>
            </div>
        </div>
    </div>
</div>
