@php
    $blocks = $settings->footer_blocks ?? [];
    $columns = $settings->footer_columns ?? 4;
    $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="{{ $academy->name_ar }}" class="h-12 w-12 rounded-lg object-contain">
                        @endif
                        <span class="text-xl font-bold {{ $textColor }}">{{ $academy->name_ar }}</span>
                    </div>
                    @if(!empty($block['content']))
                        <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ $block['content'] }}</p>
                    @elseif($settings->site_description)
                        <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ Str::limit($settings->site_description, 200) }}</p>
                    @endif
                </div>
                @break

            @case('links')
                <div>
                    <h4 class="{{ $textColor }} font-bold mb-4">{{ $block['title'] ?? 'روابط سريعة' }}</h4>
                    <ul class="space-y-2">
                        @if(!empty($block['links']))
                            @foreach($block['links'] as $link)
                                <li>
                                    <a href="{{ $link['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="#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('contact')
                <div>
                    <h4 class="{{ $textColor }} font-bold mb-4">{{ $block['title'] ?? 'تواصل معنا' }}</h4>
                    <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>
                    <h4 class="{{ $textColor }} font-bold mb-4">{{ $block['title'] ?? 'تابعنا' }}</h4>
                    @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="{{ $settings->social_links[$platform] }}" 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>
                    <h4 class="{{ $textColor }} font-bold mb-4">{{ $block['title'] ?? 'ساعات العمل' }}</h4>
                    @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="{{ $academy->name_ar }}" class="h-12 w-12 rounded-lg object-contain">
                @endif
                <span class="text-xl font-bold {{ $textColor }}">{{ $academy->name_ar }}</span>
            </div>
            @if($settings->site_description)
                <p class="{{ $mutedColor }} leading-relaxed max-w-md">{{ Str::limit($settings->site_description, 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="{{ $settings->social_links[$platform] }}" 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>
            <h4 class="{{ $textColor }} font-bold mb-4">روابط سريعة</h4>
            <ul class="space-y-2">
                @foreach($sections->take(6) as $navSection)
                    <li>
                        <a href="#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>
            <h4 class="{{ $textColor }} font-bold mb-4">تواصل معنا</h4>
            <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 --}}
<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 ?? '&copy; ' . date('Y') . ' ' . $academy->name_ar . '. جميع الحقوق محفوظة.' }}
    </p>
    <p class="{{ $mutedColor }} opacity-60 text-xs">
        مدعوم بنظام El Captain
    </p>
</div>
