@extends('dashboard.app')

@section('page_title', 'Admin Webhooks')
@section('page_subtitle', 'Monitor deliveries, failures, and replays.')

@section('dashboard_content')
    @if (session('status'))
        <div class="mb-6 rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-700 dark:border-emerald-500/30 dark:bg-emerald-500/15 dark:text-emerald-200">
            {{ session('status') }}
        </div>
    @endif

    <div class="grid gap-6 lg:grid-cols-3">
        @foreach ([
            ['label' => 'Events (total)', 'value' => number_format(\App\Models\WebhookEvent::count()), 'note' => 'All providers'],
            ['label' => 'Failures', 'value' => number_format(\App\Models\WebhookEvent::where('status', 'error')->count()), 'note' => 'Needs replay'],
            ['label' => 'Providers', 'value' => number_format(\App\Models\WebhookEvent::distinct('provider')->count('provider')), 'note' => 'Configured'],
        ] as $card)
            <div class="rounded-2xl glass-card p-5">
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">{{ $card['label'] }}</div>
                <div class="mt-3 text-3xl font-semibold text-white">{{ $card['value'] }}</div>
                <div class="mt-2 text-sm text-gray-400">{{ $card['note'] }}</div>
            </div>
        @endforeach
    </div>

    <div class="mt-8 rounded-2xl glass-card p-6">
        <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
            <div>
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Delivery log</div>
                <div class="mt-2 text-lg font-semibold text-white">Recent events</div>
            </div>
            <div class="flex flex-wrap gap-2">
                <form method="GET" class="flex flex-wrap gap-2">
                    <input type="hidden" name="sort" value="{{ $sort ?? 'id' }}">
                    <input type="hidden" name="dir" value="{{ $dir ?? 'desc' }}">
                    <select name="provider" class="rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                        <option value="">All providers</option>
                        @foreach ($providers ?? [] as $provider)
                            <option value="{{ $provider }}" @selected(($filters['provider'] ?? '') === $provider)>{{ $provider }}</option>
                        @endforeach
                    </select>
                    <select name="status" class="rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                        <option value="">All statuses</option>
                        <option value="processed" @selected(($filters['status'] ?? '') === 'processed')>Processed</option>
                        <option value="received" @selected(($filters['status'] ?? '') === 'received')>Received</option>
                        <option value="pending" @selected(($filters['status'] ?? '') === 'pending')>Pending</option>
                        <option value="error" @selected(($filters['status'] ?? '') === 'error')>Error</option>
                    </select>
                    <button class="rounded-xl border border-white/10 px-4 py-2 text-sm font-semibold text-gray-200 hover:bg-white/5 transition-colors">Filter</button>
                </form>
                <form method="POST" action="{{ route('dashboard.admin.webhooks.replay_failed') }}">
                    @csrf
                    <button class="rounded-xl border border-white/10 px-4 py-2 text-sm font-semibold text-gray-200 hover:bg-white/5 transition-colors">Replay failed</button>
                </form>
            </div>
        </div>

        <div class="mt-6 overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="text-xs uppercase tracking-[0.15em] text-gray-400">
                    <tr>
                        @php
                            $sortParam = $sort ?? 'id';
                            $dirParam = $dir ?? 'desc';
                            $toggle = fn ($field) => ($sortParam === $field && $dirParam === 'asc') ? 'desc' : 'asc';
                            $sortUrl = fn ($field) => request()->fullUrlWithQuery(['sort' => $field, 'dir' => $toggle($field)]);
                        @endphp
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('id') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Event
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('provider') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Provider
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('status') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Status
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('received_at') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Time
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/10 text-gray-300">
                    @forelse ($events ?? [] as $row)
                        <tr>
                            <td class="py-4 pr-4">
                                <div class="font-semibold text-white">#{{ $row->id }}</div>
                                <div class="text-xs text-gray-400">{{ $row->event_type ?? 'event' }}</div>
                            </td>
                            <td class="py-4 pr-4">{{ $row->provider }}</td>
                            <td class="py-4 pr-4">
                                @php
                                    $pill = $row->status === 'error'
                                        ? ['bg' => 'bg-rose-100 dark:bg-rose-500/20', 'text' => 'text-rose-800 dark:text-rose-200']
                                        : ($row->status === 'pending' || $row->status === 'received'
                                            ? ['bg' => 'bg-amber-100 dark:bg-amber-500/20', 'text' => 'text-amber-800 dark:text-amber-200']
                                            : ['bg' => 'bg-emerald-100 dark:bg-emerald-500/20', 'text' => 'text-emerald-800 dark:text-emerald-200']);
                                @endphp
                                <span class="rounded-full {{ $pill['bg'] }} px-3 py-1 text-xs font-semibold {{ $pill['text'] }}">
                                    {{ $row->status }}
                                </span>
                            </td>
                            <td class="py-4 pr-4 text-xs">{{ $row->received_at?->diffForHumans() ?? $row->created_at?->diffForHumans() }}</td>
                            <td class="py-4 pr-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('dashboard.admin.webhooks.show', $row->id) }}" class="rounded-full border border-white/10 px-3 py-1 text-xs font-semibold text-gray-200 hover:bg-white/5 transition-colors">View</a>
                                    <form method="POST" action="{{ route('dashboard.admin.webhooks.replay', $row->id) }}">
                                        @csrf
                                        <button class="rounded-full border border-white/10 px-3 py-1 text-xs font-semibold text-gray-200 hover:bg-white/5 transition-colors">Replay</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="py-6 text-center text-sm text-gray-400">No webhook events found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="mt-6">
            {{ $events->links('vendor.pagination.dashboard') }}
        </div>
    </div>
@endsection
