<div @click.stop>
    <div class="relative">
        <x-jetstream.dropdown
            align="left"
            class="full-width absolute top-1 right-auto max-w-xs overflow-x-auto"
            style="min-width: 12rem;"
        >
            {{-- Trigger Button --}}
            <x-slot name="trigger">
                @php
                    // Get active profile info to exclude from unread check
                    $activeProfileType = session('activeProfileType');
                    $activeProfileId = session('activeProfileId');
                    $activeTypeKey = null;

                    if ($activeProfileType) {
                        $activeTypeKey = match($activeProfileType) {
                            'App\\Models\\User', 'App\Models\User' => 'user',
                            'App\\Models\\Admin', 'App\Models\Admin' => 'admin',
                            'App\\Models\\Bank', 'App\Models\Bank' => 'bank',
                            'App\\Models\\Organization', 'App\Models\Organization' => 'organization',
                            default => null
                        };
                    }

                    // Check if any NON-ACTIVE profile has unread messages
                    $hasAnyUnread = false;

                    // Check user profile (only if not active)
                    if ($activeTypeKey !== 'user') {
                        $hasAnyUnread = $profilesWithUnread['user'] ?? false;
                    }

                    if (!$hasAnyUnread) {
                        foreach (['admin', 'bank', 'organization'] as $type) {
                            if (!empty($profilesWithUnread[$type])) {
                                foreach ($profilesWithUnread[$type] as $profileId => $hasUnread) {
                                    // Skip if this is the active profile
                                    if ($activeTypeKey === $type && $activeProfileId == $profileId) {
                                        continue;
                                    }
                                    if ($hasUnread) {
                                        $hasAnyUnread = true;
                                        break 2;
                                    }
                                }
                            }
                        }
                    }
                @endphp
                <span class="inline-flex rounded-md w-full">
                    <button type="button" class="inline-flex items-center px-4 py-2 w-full text-base sm:text-sm leading-4 rounded-md text-theme-primary bg-white hover:text-theme-secondary focus:outline-none transition {{ $hasAnyUnread ? 'justify-between' : '' }}">
                        <span class="flex items-center">
                            {{ __('Switch profile') }}
                            <svg class="ml-2 -mr-0.5 h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293
                                a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1
                                0 010-1.414z" clip-rule="evenodd" />
                            </svg>
                        </span>

                        @if($hasAnyUnread)
                            <span class="h-2 w-2 rounded-full bg-red-700"></span>
                        @endif
                    </button>
                </span>
            </x-slot>

            {{-- Dropdown Content --}}
            <x-slot name="content" class="shadow-2xl">
                <x-jetstream.dropdown-link
                    href="#"
                    wire:click.prevent="profileSelected(null)"
                    class="cursor-pointer flex items-center justify-between"
                >
                    <span>{{ \Illuminate\Support\Str::limit($userName, 25, '...') }}</span>

                    @if(($profilesWithUnread['user'] ?? false) && $activeTypeKey !== 'user')
                        <span class="h-2 w-2 rounded-full bg-red-700"></span>
                    @endif
                </x-jetstream.dropdown-link>

                <div class="border-t border-gray-100"></div>
                @foreach($userProfiles as $index => $userProfile)
                    @php
                        // Check if this is the active profile
                        $isActiveProfile = ($activeTypeKey === $userProfile['type'] && $activeProfileId == $userProfile['id']);
                    @endphp
                    <x-jetstream.dropdown-link
                        href="#"
                        wire:click.prevent="profileSelected({{ $index }})"
                        class="cursor-pointer flex items-center justify-between"
                    >
                        <span>{{ \Illuminate\Support\Str::limit($userProfile['name'], 25, '...') }}</span>

                        @if(($profilesWithUnread[$userProfile['type']][$userProfile['id']] ?? false) && !$isActiveProfile)
                            <span class="h-2 w-2 rounded-full bg-red-700"></span>
                        @endif
                    </x-jetstream.dropdown-link>
                @endforeach
            </x-slot>
        </x-jetstream.dropdown>
    </div>
</div>