@php
    $navBase = $navBase ?? (request()->routeIs('website.show') ? '' : route('website.show', ['slug' => $academy->slug]));
    $sections = $sections ?? collect();
    $blocks = $settings->footer_blocks ?? [];
    $columns = $settings->footer_columns ?? 4;

    // The accent band below carries the copyright when it is configured; showing
    // it here as well would print the same line twice.
    $hasPoweredBar = filled($settings->footer_powered_by ?? null);

    $isAr = app()->getLocale() === 'ar';
    $academyName = $isAr ? ($academy->name_ar ?: $academy->name) : ($academy->name ?: $academy->name_ar);
    $siteBlurb = $isAr
        ? ($settings->site_description ?: $settings->site_description_en)
        : ($settings->site_description_en ?: $settings->site_description);

    // A footer column heading is authored content, so it may be stored either as
    // a plain string or as an ['ar' => …, 'en' => …] pair.
    $blockTitle = fn ($block, $fallback = '') => website_text($block['title'] ?? null) ?: $fallback;
    $gridClass = match($columns) {
        2 => 'grid-cols-1 md:grid-cols-2',
        3 => 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
        default => 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
    };
@endphp

<div class="grid {{ $gridClass }} gap-10">
    @forelse($blocks as $block)
        @switch($block['type'] ?? '')
            @case('about')
                <div class="{{ count($blocks) <= 2 ? 'md:col-span-2' : '' }}">
                    <div class="flex items-center gap-3 mb-4">
                        @if($academy->logo_path)
                            <img src="{{ asset('storage/' . $academy->logo_path) }}" alt="{{ $academyName }}" class="h-14 w-auto max-w-[10rem] object-contain">
                        @endif
                        @if($settings->navbar_secondary_logo_path)
                            <span class="h-9 w-px bg-current opacity-25" aria-hidden="true"></span>
                            <img src="{{ asset('storage/' . $settings->navbar_secondary_logo_path) }}" alt="" aria-hidden="true" class="h-12 w-auto max-w-[10rem] object-contain">
                        @elseif(! $academy->logo_path)
                            <span class="text-xl font-bold {{ $textColor }}">{{ $academyName }}</span>
                        @endif
                    </div>
                    @if(!empty($block['content']))
                        <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ website_text($block['content']) }}</p>
                    @elseif($siteBlurb)
                        <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ Str::limit($siteBlurb, 200) }}</p>
                    @endif

                    @if(is_array($settings->social_links ?? null) && count(array_filter($settings->social_links)))
                        <div class="flex items-center gap-4 mt-6">
                            @foreach(['facebook', 'instagram', 'youtube', 'tiktok', 'twitter'] as $platform)
                                @if(!empty($settings->social_links[$platform]))
                                    <a href="{{ safe_url($settings->social_links[$platform]) ?: url('/') }}" target="_blank" rel="noopener noreferrer"
                                       class="{{ $textColor }} transition hover:text-[var(--site-accent)]"
                                       aria-label="{{ $platform }}">
                                        @include("website.icons.{$platform}")
                                    </a>
                                @endif
                            @endforeach
                            @if($settings->whatsapp_number)
                                <a href="{{ whatsapp_link($settings->whatsapp_number) }}" target="_blank" rel="noopener noreferrer"
                                   class="{{ $textColor }} transition hover:text-[var(--site-accent)]" aria-label="whatsapp">
                                    <x-website.icon name="whatsapp" class="w-5 h-5" />
                                </a>
                            @endif
                        </div>
                    @endif
                </div>
                @break

            @case('links')
                <div>
                    <h3 class="{{ $textColor }} font-bold mb-4">{{ $blockTitle($block, 'روابط سريعة') }}</h3>
                    <ul class="space-y-2">
                        @if(!empty($block['links']))
                            @foreach($block['links'] as $link)
                                <li>
                                    <a href="{{ safe_url($link['url'] ?? null) ?: url('/') }}" class="{{ $mutedColor }} hover:text-[var(--site-accent)] transition-colors text-sm">
                                        {{ $link['label'] ?? '' }}
                                    </a>
                                </li>
                            @endforeach
                        @else
                            @foreach($sections->take(6) as $navSection)
                                <li>
                                    <a href="{{ $navBase ?? '' }}#section-{{ $navSection->section_key->value }}" class="{{ $mutedColor }} hover:text-[var(--site-accent)] transition-colors text-sm">
                                        {{ $navSection->title ?? $navSection->section_key->label() }}
                                    </a>
                                </li>
                            @endforeach
                        @endif
                    </ul>
                </div>
                @break

            @case('spacer')
                {{-- Holds a grid cell so the columns below start where intended. --}}
                <div aria-hidden="true"></div>
                @break

            @case('menu')
                {{-- A column of links from an authored menu, so the footer and the
                     navbar cannot drift apart as pages are added or renamed. --}}
                <div>
                    <h3 class="{{ $textColor }} font-bold mb-4">{{ $blockTitle($block, '') }}</h3>
                    @include('website.partials.site-menu', [
                        'menuKey' => $block['menu_key'] ?? 'footer',
                        'layout' => 'stacked',
                        'linkClass' => $mutedColor.' hover:text-[var(--site-accent)] text-sm',
                    ])
                </div>
                @break

            @case('contact')
                <div>
                    <h3 class="{{ $textColor }} font-bold mb-4">{{ $blockTitle($block, 'تواصل معنا') }}</h3>
                    <ul class="space-y-3 text-sm">
                        @if($academy->phone)
                            <li class="flex items-center gap-2 {{ $mutedColor }}">
                                <svg class="w-4 h-4 text-[var(--site-accent)] shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
                                <span dir="ltr">{{ $academy->phone }}</span>
                            </li>
                        @endif
                        @if($academy->email)
                            <li class="flex items-center gap-2 {{ $mutedColor }}">
                                <svg class="w-4 h-4 text-[var(--site-accent)] shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/></svg>
                                <span>{{ $academy->email }}</span>
                            </li>
                        @endif
                        @if($settings->whatsapp_number)
                            <li class="flex items-center gap-2 {{ $mutedColor }}">
                                <svg class="w-4 h-4 text-green-400 shrink-0" fill="currentColor" viewBox="0 0 24 24"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/></svg>
                                <span dir="ltr">{{ $settings->whatsapp_number }}</span>
                            </li>
                        @endif
                    </ul>
                </div>
                @break

            @case('social')
                <div>
                    <h3 class="{{ $textColor }} font-bold mb-4">{{ $blockTitle($block, 'تابعنا') }}</h3>
                    @if(is_array($settings->social_links ?? null) && count(array_filter($settings->social_links)))
                        <div class="flex items-center gap-3 flex-wrap">
                            @foreach(['facebook', 'instagram', 'twitter', 'youtube', 'tiktok'] as $platform)
                                @if(!empty($settings->social_links[$platform]))
                                    <a href="{{ safe_url($settings->social_links[$platform] ?? null) ?: url('/') }}" target="_blank" rel="noopener"
                                       class="w-10 h-10 rounded-lg bg-white/5 border {{ $borderColor }} flex items-center justify-center {{ $mutedColor }} hover:text-[var(--site-accent)] hover:border-[var(--site-accent)] transition-all"
                                       aria-label="{{ $platform }}">
                                        @include("website.icons.{$platform}")
                                    </a>
                                @endif
                            @endforeach
                        </div>
                    @endif
                </div>
                @break

            @case('hours')
                <div>
                    <h3 class="{{ $textColor }} font-bold mb-4">{{ $blockTitle($block, 'ساعات العمل') }}</h3>
                    @if(!empty($block['hours']))
                        <ul class="space-y-2 text-sm">
                            @foreach($block['hours'] as $hour)
                                <li class="flex items-center justify-between {{ $mutedColor }}">
                                    <span>{{ $hour['day'] ?? '' }}</span>
                                    <span dir="ltr">{{ $hour['time'] ?? '' }}</span>
                                </li>
                            @endforeach
                        </ul>
                    @endif
                </div>
                @break
        @endswitch
    @empty
        {{-- Default footer when no blocks configured --}}
        <div class="lg:col-span-2">
            <div class="flex items-center gap-3 mb-4">
                @if($academy->logo_path)
                    <img src="{{ asset('storage/' . $academy->logo_path) }}" alt="{{ $academyName }}" class="h-12 w-12 rounded-lg object-contain">
                @endif
                <span class="text-xl font-bold {{ $textColor }}">{{ $academyName }}</span>
            </div>
            @if($siteBlurb)
                <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ Str::limit($siteBlurb, 200) }}</p>
            @endif
            @if(is_array($settings->social_links ?? null) && count(array_filter($settings->social_links)))
                <div class="flex items-center gap-3 mt-6">
                    @foreach(['facebook', 'instagram', 'twitter', 'youtube', 'tiktok'] as $platform)
                        @if(!empty($settings->social_links[$platform]))
                            <a href="{{ safe_url($settings->social_links[$platform] ?? null) ?: url('/') }}" target="_blank" rel="noopener"
                               class="w-10 h-10 rounded-lg bg-white/5 border {{ $borderColor }} flex items-center justify-center {{ $mutedColor }} hover:text-[var(--site-accent)] hover:border-[var(--site-accent)] transition-all"
                               aria-label="{{ $platform }}">
                                @include("website.icons.{$platform}")
                            </a>
                        @endif
                    @endforeach
                </div>
            @endif
        </div>
        <div>
            <h3 class="{{ $textColor }} font-bold mb-4">روابط سريعة</h3>
            <ul class="space-y-2">
                @foreach($sections->take(6) as $navSection)
                    <li>
                        <a href="{{ $navBase ?? '' }}#section-{{ $navSection->section_key->value }}" class="{{ $mutedColor }} hover:text-[var(--site-accent)] transition-colors text-sm">
                            {{ $navSection->title ?? $navSection->section_key->label() }}
                        </a>
                    </li>
                @endforeach
            </ul>
        </div>
        <div>
            <h3 class="{{ $textColor }} font-bold mb-4">تواصل معنا</h3>
            <ul class="space-y-3 text-sm">
                @if($academy->phone)
                    <li class="flex items-center gap-2 {{ $mutedColor }}">
                        <svg class="w-4 h-4 text-[var(--site-accent)] shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
                        <span dir="ltr">{{ $academy->phone }}</span>
                    </li>
                @endif
                @if($academy->email)
                    <li class="flex items-center gap-2 {{ $mutedColor }}">
                        <svg class="w-4 h-4 text-[var(--site-accent)] shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/></svg>
                        <span>{{ $academy->email }}</span>
                    </li>
                @endif
                @if($settings->whatsapp_number)
                    <li class="flex items-center gap-2 {{ $mutedColor }}">
                        <svg class="w-4 h-4 text-green-400 shrink-0" fill="currentColor" viewBox="0 0 24 24"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/></svg>
                        <span dir="ltr">{{ $settings->whatsapp_number }}</span>
                    </li>
                @endif
            </ul>
        </div>
    @endforelse
</div>

{{-- Bottom Bar --}}
@unless ($hasPoweredBar)
    <div class="mt-12 pt-8 {{ $borderColor }} border-t flex flex-col sm:flex-row items-center justify-between gap-4">
        <p class="{{ $mutedColor }} text-sm">
            {{ $settings->footer_bottom_text ?: '© '.date('Y').' '.$academyName.'. '.__('جميع الحقوق محفوظة.') }}
        </p>
        <p class="{{ $mutedColor }} opacity-60 text-xs">
            {{ __('مدعوم بنظام El Captain') }}
        </p>
    </div>
@endunless
