@php
    use App\Models\User;
    use App\Services\Localization\LocaleResolver;

    $plane = $plane ?? 'admin';
    $showPreference = (bool) ($showPreference ?? true);
    $embedded = (bool) ($embedded ?? false);

    /** @var LocaleResolver $localeResolver */
    $localeResolver = app(LocaleResolver::class);
    $localeContext = request()->attributes->get(LocaleResolver::REQUEST_ATTRIBUTE);
    $localeContext = is_array($localeContext) ? $localeContext : $localeResolver->resolve(request(), $plane);
    $localeOptions = LocaleResolver::localeOptions();
    $currentLocale = (string) ($localeContext['locale'] ?? 'en');
    $source = (string) ($localeContext['source'] ?? LocaleResolver::SOURCE_SYSTEM_DEFAULT);
    $sourceLabel = __('localization.source.'.$source);
    $user = auth()->user();
    $preferredLocale = $user instanceof User ? $user->preferred_locale : null;
@endphp

<div class="{{ $embedded ? 'border-l border-gray-200 dark:border-white/10' : 'inline-flex rounded-lg border border-gray-200 bg-white text-sm dark:border-white/10 dark:bg-white/5' }}">
    <x-filament::dropdown placement="bottom-end" teleport width="sm">
        <x-slot name="trigger">
            <button
                type="button"
                aria-label="{{ __('localization.shell.language') }}"
                class="inline-flex items-center gap-1.5 rounded-r-lg px-2 py-1.5 text-sm transition hover:bg-gray-50 dark:hover:bg-white/10"
            >
                <x-filament::icon icon="heroicon-o-language" class="h-4 w-4 text-gray-400 dark:text-gray-500" />
                <span class="font-medium text-gray-700 dark:text-gray-200">{{ strtoupper($currentLocale) }}</span>
                <x-filament::icon icon="heroicon-m-chevron-down" class="h-3.5 w-3.5 text-gray-400 dark:text-gray-500" />
            </button>
        </x-slot>

        <x-filament::dropdown.list>
            <div class="space-y-3 px-3 py-2">
                <div class="space-y-1">
                    <div class="text-xs font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500">
                        {{ __('localization.shell.current_language') }}
                    </div>
                    <div class="rounded-lg bg-gray-50 px-3 py-2 dark:bg-white/5">
                        <div class="text-sm font-medium text-gray-950 dark:text-white">
                            {{ $localeOptions[$currentLocale] ?? strtoupper($currentLocale) }}
                        </div>
                        <div class="text-xs text-gray-500 dark:text-gray-400">
                            {{ __('localization.shell.language_source', ['source' => $sourceLabel]) }}
                        </div>
                    </div>
                </div>

                <div class="border-t border-gray-200 dark:border-white/10"></div>

                <form method="POST" action="{{ route('localization.override.update') }}" class="space-y-2">
                    @csrf
                    <label class="text-xs font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500" for="tenantpilot-locale-override-{{ $plane }}">
                        {{ __('localization.shell.temporary_override') }}
                    </label>
                    <x-filament::input.wrapper class="w-full">
                        <x-filament::input.select
                            id="tenantpilot-locale-override-{{ $plane }}"
                            name="locale"
                        >
                            @foreach ($localeOptions as $locale => $label)
                                <option value="{{ $locale }}" @selected($currentLocale === $locale)>{{ $label }}</option>
                            @endforeach
                        </x-filament::input.select>
                    </x-filament::input.wrapper>
                    <button type="submit" class="w-full rounded-lg bg-primary-600 px-3 py-1.5 text-sm font-medium text-white transition hover:bg-primary-500">
                        {{ __('localization.shell.switch_language') }}
                    </button>
                </form>

                @if ($source === LocaleResolver::SOURCE_EXPLICIT_OVERRIDE)
                    <form method="POST" action="{{ route('localization.override.clear') }}">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="w-full rounded-lg px-3 py-1.5 text-left text-xs font-medium text-gray-500 transition hover:bg-gray-50 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-white/5 dark:hover:text-gray-200">
                            {{ __('localization.shell.clear_override') }}
                        </button>
                    </form>
                @endif

                @if ($showPreference && $user instanceof User)
                    <div class="border-t border-gray-200 dark:border-white/10"></div>

                    <form method="POST" action="{{ route('localization.preference.update') }}" class="space-y-2">
                        @csrf
                        <label class="text-xs font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500" for="tenantpilot-locale-preference-{{ $plane }}">
                            {{ __('localization.shell.personal_preference') }}
                        </label>
                        <x-filament::input.wrapper class="w-full">
                            <x-filament::input.select
                                id="tenantpilot-locale-preference-{{ $plane }}"
                                name="preferred_locale"
                            >
                                <option value="" @selected($preferredLocale === null)>{{ __('localization.shell.inherit_workspace') }}</option>
                                @foreach ($localeOptions as $locale => $label)
                                    <option value="{{ $locale }}" @selected($preferredLocale === $locale)>{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>
                        <button type="submit" class="w-full rounded-lg px-3 py-1.5 text-sm font-medium text-primary-600 transition hover:bg-primary-50 hover:text-primary-500 dark:text-primary-400 dark:hover:bg-primary-500/10 dark:hover:text-primary-300">
                            {{ __('localization.shell.save_preference') }}
                        </button>
                    </form>
                @endif
            </div>
        </x-filament::dropdown.list>
    </x-filament::dropdown>
</div>
