<div class="relative" wire:poll.30s="loadNotifications">
    <!-- Bell button -->
    <div class="dropdown dropdown-end">
        <label tabindex="0" class="btn btn-ghost btn-circle" role="button">
            <div class="indicator">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
                </svg>
                @if($unreadCount > 0)
                    <span class="badge badge-xs badge-error indicator-item">
                        {{ $unreadCount > 99 ? '99+' : $unreadCount }}
                    </span>
                @endif
            </div>
        </label>

        <div tabindex="0" class="dropdown-content z-[50] menu p-0 shadow-lg bg-base-100 rounded-box w-80 mt-1 border border-base-200">
            <!-- Header -->
            <div class="flex items-center justify-between px-4 py-3 border-b border-base-200">
                <span class="font-semibold text-base-content">{{ __('Notifications') }}</span>
                @if($unreadCount > 0)
                    <button wire:click="markAllAsRead"
                        class="text-xs text-primary hover:underline focus:outline-none">
                        {{ __('Mark all as read') }}
                    </button>
                @endif
            </div>

            <!-- Notification list -->
            <ul class="max-h-80 overflow-y-auto divide-y divide-base-200">
                @forelse($notifications as $notification)
                    @php
                        $data = is_array($notification['data']) ? $notification['data'] : json_decode($notification['data'], true);
                        $isUnread = is_null($notification['read_at']);
                        $createdAt = \Carbon\Carbon::parse($notification['created_at']);
                    @endphp
                    <li class="flex items-start gap-3 px-4 py-3 {{ $isUnread ? 'bg-primary/5' : '' }} hover:bg-base-200 transition-colors">
                        <!-- Dot indicator -->
                        <div class="mt-1 shrink-0">
                            @if($isUnread)
                                <span class="inline-block w-2 h-2 rounded-full bg-primary"></span>
                            @else
                                <span class="inline-block w-2 h-2 rounded-full bg-base-300"></span>
                            @endif
                        </div>

                        <!-- Content -->
                        <div class="flex-1 min-w-0">
                            <p class="text-sm text-base-content leading-snug">
                                {{ $data['message'] ?? __('Notification') }}
                            </p>
                            <p class="text-xs text-base-content/50 mt-1">
                                {{ $createdAt->diffForHumans() }}
                            </p>
                        </div>

                        <!-- Mark as read -->
                        @if($isUnread)
                            <button wire:click="markAsRead('{{ $notification['id'] }}')"
                                class="shrink-0 text-base-content/40 hover:text-primary focus:outline-none"
                                title="{{ __('Mark as read') }}">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" viewBox="0 0 20 20" fill="currentColor">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
                                </svg>
                            </button>
                        @endif
                    </li>
                @empty
                    <li class="px-4 py-8 text-center text-sm text-base-content/50">
                        {{ __('No notifications') }}
                    </li>
                @endforelse
            </ul>

            <!-- Footer -->
            @if(count($notifications) > 0 && $unreadCount > 0)
                <div class="border-t border-base-200 px-4 py-2 text-center">
                    <button wire:click="markAllAsRead"
                        class="btn btn-ghost btn-xs w-full text-primary">
                        {{ __('Mark all as read') }}
                    </button>
                </div>
            @endif
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('livewire:load', () => {
            const userId = {{ auth()->id() ?? 'null' }};
            if (userId && window.Echo) {
                window.Echo.private(`user.${userId}`)
                    .notification((notification) => {
                        Livewire.emit('notification-received', notification.data.message ?? 'Nueva notificación');
                    });
            }
        });
    </script>
    @endpush
</div>
