@php
    $statusClasses = [
        'success'    => ['badge' => 'bg-green-50 text-green-700',   'dot' => 'bg-green-500'],
        'failed'     => ['badge' => 'bg-red-50 text-red-700',       'dot' => 'bg-red-500'],
        'error'      => ['badge' => 'bg-red-50 text-red-700',       'dot' => 'bg-red-500'],
        'conflict'   => ['badge' => 'bg-amber-50 text-amber-700',   'dot' => 'bg-amber-400'],
        'duplicate'  => ['badge' => 'bg-blue-50 text-blue-700',     'dot' => 'bg-blue-400'],
        'processing' => ['badge' => 'bg-purple-50 text-purple-700', 'dot' => 'bg-purple-400'],
    ];

    $typeBadges = [
        'event'        => ['#', t('agent.type.event'),   '#4F46E5', '#EEF2FF'],
        'event_update' => ['✎', t('agent.type.event'),   '#4F46E5', '#EEF2FF'],
        'note'         => ['#', t('agent.type.note'),    '#B45309', '#FFFBEB'],
        'note_update'  => ['✎', t('agent.type.note'),    '#B45309', '#FFFBEB'],
        'task'         => ['#', t('agent.type.task'),    '#065F46', '#ECFDF5'],
        'task_update'  => ['✎', t('agent.type.task'),    '#065F46', '#ECFDF5'],
        'contact'      => ['#', t('agent.type.contact'), '#1E40AF', '#EFF6FF'],
        'email'        => ['#', t('agent.type.email'),   '#6B21A8', '#FAF5FF'],
        'chat'         => ['#', 'Chat',                  '#374151', '#F3F4F6'],
        'multi'        => ['#', t('agent.type.multi'),   '#9D174D', '#FDF2F8'],
        'bonus'        => ['#', t('agent.type.bonus'),   '#065F46', '#ECFDF5'],
        'system'       => ['#', t('agent.type.system'),  '#374151', '#F9FAFB'],
    ];

    $statusLabels = [
        'success'    => t('agent.status.success'),
        'failed'     => t('agent.status.failed'),
        'error'      => t('agent.status.error'),
        'conflict'   => t('agent.status.conflict'),
        'duplicate'  => t('agent.status.duplicate'),
        'processing' => t('agent.status.processing'),
    ];
@endphp

<div class="space-y-6">

    {{-- Header --}}
    <div>
        <h1 class="text-xl font-semibold text-gray-800">{{ t('admin.title') }}</h1>
        <p class="text-sm text-gray-500 mt-1">{{ t('admin.subtitle') }}</p>
    </div>

    {{-- Metric Cards --}}
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-indigo-50 flex items-center justify-center">
                    <x-heroicon-s-users class="w-5 h-5 text-indigo-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($totalUsers) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.total_users') }}</p>
                </div>
            </div>
        </div>

        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-green-50 flex items-center justify-center">
                    <x-heroicon-s-check-circle class="w-5 h-5 text-green-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($activeUsers) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.active_users') }}</p>
                </div>
            </div>
        </div>

        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-blue-50 flex items-center justify-center">
                    <x-heroicon-s-user-plus class="w-5 h-5 text-blue-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($newThisMonth) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.new_this_month') }}</p>
                </div>
            </div>
        </div>

        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-purple-50 flex items-center justify-center">
                    <x-heroicon-s-star class="w-5 h-5 text-purple-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($proUsers) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.pro_users') }}</p>
                </div>
            </div>
        </div>
    </div>

    {{-- Cost Cards --}}
    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-amber-50 flex items-center justify-center">
                    <x-heroicon-s-bolt class="w-5 h-5 text-amber-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($totalCreditsUsed) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.credits_month') }}</p>
                </div>
            </div>
        </div>

        <div class="bg-white border border-gray-100 rounded-xl shadow-sm p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg bg-red-50 flex items-center justify-center">
                    <x-heroicon-s-currency-dollar class="w-5 h-5 text-red-600"/>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">${{ number_format($totalCost, 2) }}</p>
                    <p class="text-xs text-gray-500">{{ t('admin.costs_month') }}</p>
                </div>
            </div>
        </div>
    </div>

    {{-- Recent Logs --}}
    <div>
        <div class="flex items-center justify-between mb-3">
            <h2 class="text-sm font-semibold text-gray-700">{{ t('admin.last_activities') }}</h2>
            <span class="text-xs text-gray-400">{{ t('admin.last_10') }}</span>
        </div>

        @if($recentLogs->isEmpty())
            <div class="bg-white border border-gray-100 rounded-xl shadow-sm py-12 flex flex-col items-center gap-3">
                <div class="w-12 h-12 rounded-full bg-gray-100 flex items-center justify-center">
                    <x-heroicon-o-cpu-chip class="w-6 h-6 text-gray-400"/>
                </div>
                <p class="text-sm font-medium text-gray-600">{{ t('admin.no_logs') }}</p>
            </div>
        @else
            <div class="bg-white border border-gray-100 rounded-xl shadow-sm divide-y divide-gray-50">
                @foreach($recentLogs as $log)
                    @php
                        $sc    = $statusClasses[$log->status] ?? ['badge' => 'bg-gray-100 text-gray-500', 'dot' => 'bg-gray-400'];
                        $badge = $typeBadges[$log->type]      ?? ['#', ucfirst($log->type ?? '?'), '#374151', '#F9FAFB'];

                        $dur = match(true) {
                            $log->duration_ms === null       => '—',
                            $log->duration_ms >= 1000        => round($log->duration_ms / 1000, 1) . 's',
                            default                          => $log->duration_ms . 'ms',
                        };
                    @endphp

                    <div class="px-5 py-4 hover:bg-gray-50/50 transition-colors" wire:key="log-{{ $log->id }}">
                        <div class="flex items-start gap-4">

                            {{-- Status Dot --}}
                            <div class="mt-1.5 shrink-0">
                                <span class="block w-2 h-2 rounded-full {{ $sc['dot'] }}"></span>
                            </div>

                            {{-- Content --}}
                            <div class="flex-1 min-w-0">

                                {{-- Input + Datum --}}
                                <div class="flex items-start justify-between gap-3">
                                    <p class="text-sm text-gray-800 leading-snug truncate">{{ $log->input }}</p>
                                    <span class="text-[11px] text-gray-300 whitespace-nowrap shrink-0">
                                        {{ $log->created_at?->format('d.m. H:i') }}
                                    </span>
                                </div>

                                {{-- Badges + Metriken --}}
                                <div class="flex flex-wrap items-center gap-2 mt-2">

                                    {{-- User --}}
                                    @if($log->user)
                                        <a href="{{ route('admin.users.detail', $log->user) }}" wire:navigate
                                           class="flex items-center gap-1.5 text-[11px] text-indigo-600 hover:text-indigo-700 font-medium">
                                            <div class="w-4 h-4 rounded-full bg-indigo-100 text-indigo-700 flex items-center justify-center text-[8px] font-bold shrink-0">
                                                {{ strtoupper(substr($log->user->name, 0, 1)) }}
                                            </div>
                                            {{ $log->user->name }}
                                        </a>
                                        <span class="text-gray-200">|</span>
                                    @endif

                                    {{-- Typ --}}
                                    <span style="display:inline-flex;align-items:center;gap:4px;padding:2px 8px;border-radius:20px;font-size:11px;font-weight:500;background:{{ $badge[3] }};color:{{ $badge[2] }};border:1px solid {{ $badge[2] }}22;">
                                        <span style="width:5px;height:5px;border-radius:50%;background:{{ $badge[2] }};display:inline-block;flex-shrink:0;"></span>
                                        {{ $badge[1] }}{{ $badge[0] === '✎' ? ' ✎' : '' }}
                                    </span>

                                    {{-- Status --}}
                                    <span class="text-[10px] font-medium {{ $sc['badge'] }} px-1.5 py-0.5 rounded-md">
                                        {{ $statusLabels[$log->status] ?? $log->status }}
                                    </span>

                                    <span class="text-gray-200">|</span>

                                    {{-- Credits --}}
                                    <span class="flex items-center gap-1 text-[11px] {{ $log->credits < 0 ? 'text-green-500' : 'text-gray-400' }}">
                                        <x-heroicon-o-bolt class="w-3 h-3 text-amber-400"/>
                                        {{ $log->credits }} Credits
                                    </span>

                                    {{-- Tokens --}}
                                    @if($log->total_tokens > 0)
                                        <span class="flex items-center gap-1 text-[11px] text-gray-400">
                                            <x-heroicon-o-document-text class="w-3 h-3 text-gray-300"/>
                                            {{ number_format($log->total_tokens) }}
                                            <span class="text-gray-300">({{ number_format($log->prompt_tokens) }}↑ {{ number_format($log->completion_tokens) }}↓)</span>
                                        </span>
                                    @endif

                                    {{-- Dauer --}}
                                    <span class="flex items-center gap-1 text-[11px] text-gray-400">
                                        <x-heroicon-o-clock class="w-3 h-3 text-gray-300"/>
                                        {{ $dur }}
                                    </span>

                                    {{-- Kosten --}}
                                    @if($log->cost_usd > 0)
                                        <span class="text-[11px] text-gray-400">${{ number_format($log->cost_usd, 4) }}</span>
                                    @endif

                                    {{-- Model --}}
                                    @if($log->model)
                                        <span class="text-[10px] text-gray-300 bg-gray-50 border border-gray-100 px-1.5 py-0.5 rounded-md font-mono">
                                            {{ $log->model }}
                                        </span>
                                    @endif
                                </div>

                                {{-- Fehler-Ausgabe --}}
                                @if(in_array($log->status, ['failed', 'error']) && !empty($log->output['error']))
                                    <p class="text-[11px] text-red-400 mt-1.5 truncate">
                                        <x-heroicon-o-exclamation-triangle class="w-3 h-3 inline-block mr-0.5"/>
                                        {{ $log->output['error'] }}
                                    </p>
                                @endif
                            </div>

                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>

</div>
