{{ $playlistsData := index hugo.Data "playlists" }}
{{ $playlists := index $playlistsData "playlists" }}

{{ if $playlists }}
<section id="playlists" class="section">
    <h2>Playlists</h2>
    
    <div class="playlists-grid">
        {{ range $playlistIndex, $playlist := $playlists }}
            {{/* Debug: show the type and data */}}
            {{/*
            <div style="color:var(--accent-yellow);font-size:0.6rem;background:rgba(255,215,0,0.1);padding:0.3rem;margin:0.3rem 0;">
                Playlist {{ $playlistIndex }}: Type = {{ printf "%T" $playlist }}
            </div>
            */}}
            
            {{/* Get the actual playlist object - it's the first element of the array */}}
            {{ $playlistObj := index $playlist 0 }}
            
            {{ if $playlistObj }}
                {{ $name := index $playlistObj "name" | default "Untitled Playlist" }}
                {{ $description := index $playlistObj "description" | default "" }}
                {{ $image := index $playlistObj "image" | default "" }}
                {{ $tracks := index $playlistObj "tracks" | default slice }}
                
                <div class="playlist-card">
                    {{ if $image }}
                    <img src="{{ $image }}" alt="{{ $name }}" class="playlist-image" loading="lazy">
                    {{ end }}
                    
                    <div class="playlist-info">
                        <h3>{{ $name }}</h3>
                        {{ if $description }}
                        <p class="playlist-description">{{ $description }}</p>
                        {{ end }}
                        {{ if $tracks }}
                        <span class="playlist-track-count">{{ len $tracks }} tracks</span>
                        {{ end }}
                    </div>
                    
                    {{ if $tracks }}
                    <div class="playlist-tracks">
                        <h4>Tracks</h4>
                        <div class="tracks-list">
                            {{ range $track := $tracks }}
                            {{ $trackTitle := index $track "title" | default "Untitled" }}
                            {{ $trackArtist := index $track "artist" | default "Unknown Artist" }}
                            {{ $trackAlbum := index $track "album" | default "" }}
                            {{ $trackImage := index $track "image" | default "" }}
                            {{ $trackUrl := index $track "url" | default "" }}
                            {{ $trackDuration := index $track "duration" | default "" }}
                            {{ $trackTags := index $track "tags" | default slice }}
                            
                            <div class="track-item">
                                {{ if $trackImage }}
                                <img src="{{ $trackImage }}" alt="{{ $trackTitle }}" class="track-thumbnail" loading="lazy">
                                {{ end }}
                                <div class="track-info">
                                    <span class="track-title">{{ $trackTitle }}</span>
                                    <span class="track-artist">{{ $trackArtist }}</span>
                                    {{ if $trackAlbum }}
                                    <span class="track-album">{{ $trackAlbum }}</span>
                                    {{ end }}
                                    {{ if $trackDuration }}
                                    <span class="track-duration">{{ $trackDuration }}s</span>
                                    {{ end }}
                                </div>
                                {{ if $trackUrl }}
                                <audio controls preload="none" class="track-player">
                                    <source src="{{ $trackUrl }}" type="audio/mpeg">
                                </audio>
                                {{ end }}
                                {{ if $trackTags }}
                                <div class="track-tags">
                                    {{ range $trackTags }}
                                    <span class="tag"><span>{{ . }}</span></span>
                                    {{ end }}
                                </div>
                                {{ end }}
                            </div>
                            {{ end }}
                        </div>
                    </div>
                    {{ end }}
                </div>
            {{ end }}
        {{ end }}
    </div>
</section>
{{ end }}
