@extends('layouts.admin', ['heading' => 'API Management', 'subheading' => 'Keys, IP whitelist, and logs'])

@section('content')
    @if($newKey)
        <div class="mb-4 rounded-2xl border border-amber-200 bg-amber-50 p-4 text-amber-800">
            New API Key: <span class="font-semibold">{{ $newKey }}</span> (copy and store securely)
        </div>
    @endif

    <div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Generate API Key</div>
            <form method="POST" action="{{ route('admin.api.keys.store') }}" class="space-y-3">
                @csrf
                <input name="name" placeholder="Key name" class="w-full rounded border p-2" required>
                <button class="w-full rounded bg-slate-900 px-4 py-2 text-sm text-white">Generate</button>
            </form>
        </div>

        <div class="admin-card p-5 xl:col-span-2">
            <div class="mb-4 text-sm font-semibold text-slate-700">API Keys</div>
            <div class="space-y-4">
                @forelse($keys as $key)
                    <div class="rounded-xl border border-slate-200 p-4">
                        <div class="flex items-center justify-between">
                            <div>
                                <div class="font-semibold">{{ $key->name }}</div>
                                <div class="text-xs text-slate-500">Created: {{ optional($key->created_at)->format('Y-m-d') }}</div>
                            </div>
                            <span class="admin-badge {{ $key->is_active ? 'success' : 'danger' }}">
                                {{ $key->is_active ? 'Active' : 'Revoked' }}
                            </span>
                        </div>
                        <div class="mt-3">
                            <div class="text-xs text-slate-500">IP Whitelist</div>
                            <div class="mt-2 flex flex-wrap gap-2">
                                @foreach($key->ips as $ip)
                                    <form method="POST" action="{{ route('admin.api.keys.ips.delete', [$key, $ip]) }}">
                                        @csrf
                                        <button class="rounded bg-slate-100 px-2 py-1 text-xs text-slate-700">{{ $ip->ip_address }} ✕</button>
                                    </form>
                                @endforeach
                            </div>
                            <form method="POST" action="{{ route('admin.api.keys.ips', $key) }}" class="mt-2 flex gap-2">
                                @csrf
                                <input name="ip_address" placeholder="IP address" class="rounded border p-1 text-xs">
                                <button class="rounded bg-emerald-600 px-2 py-1 text-xs text-white">Add IP</button>
                            </form>
                        </div>
                        <div class="mt-3">
                            <form method="POST" action="{{ route('admin.api.keys.revoke', $key) }}">
                                @csrf
                                <button class="rounded bg-rose-600 px-3 py-1 text-xs text-white">Revoke</button>
                            </form>
                        </div>
                    </div>
                @empty
                    <div class="text-sm text-slate-500">No API keys yet.</div>
                @endforelse
            </div>
        </div>
    </div>

    <div class="mt-6 admin-card p-5">
        <div class="mb-4 text-sm font-semibold text-slate-700">Recent API Logs</div>
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-100 text-left">
                    <tr>
                        <th class="p-3">Endpoint</th>
                        <th class="p-3">Method</th>
                        <th class="p-3">Status</th>
                        <th class="p-3">IP</th>
                        <th class="p-3">Time (ms)</th>
                        <th class="p-3">At</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($logs as $log)
                        <tr class="border-t">
                            <td class="p-3">{{ $log->endpoint }}</td>
                            <td class="p-3">{{ $log->method }}</td>
                            <td class="p-3">{{ $log->status }}</td>
                            <td class="p-3">{{ $log->ip }}</td>
                            <td class="p-3">{{ $log->response_time_ms }}</td>
                            <td class="p-3">{{ optional($log->created_at)->format('Y-m-d H:i') }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="p-4 text-center text-slate-500">No logs yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
@endsection
