@php
    $castItems = collect($castItems ?? [])->take(12);
    $mediaService = app(\App\Services\MediaStorageService::class);
@endphp

@if ($castItems->isNotEmpty())
    <section class="top-cast-section" aria-label="Top cast">
        <div class="top-cast-heading"><span></span><h2>Top Cast</h2></div>
        <div class="top-cast-list">
            @foreach ($castItems as $cast)
                @php
                    $castName = trim((string) ($cast['name'] ?? 'Unknown'));
                    $character = trim((string) ($cast['character'] ?? ''));
                    $castImage = $mediaService->url($cast['image'] ?? null);
                    $initials = collect(preg_split('/\s+/', $castName))->filter()->take(2)->map(fn ($part) => mb_strtoupper(mb_substr($part, 0, 1)))->implode('');
                @endphp
                <article class="top-cast-card">
                    <div class="top-cast-avatar">
                        @if ($castImage)
                            <img src="{{ $castImage }}" alt="{{ $castName }}" loading="lazy">
                        @else
                            <span>{{ $initials ?: '?' }}</span>
                        @endif
                    </div>
                    <strong title="{{ $castName }}">{{ $castName }}</strong>
                    @if ($character !== '')<small title="{{ $character }}">{{ $character }}</small>@endif
                </article>
            @endforeach
        </div>
    </section>
@endif
