@extends('layouts.app')

@section('title', $playlist->name . ' | ' . config('app.name'))

@push('head')
@php
    $plDesc = trim($playlist->description ?? '');
    if ($plDesc === '') {
        $plDesc = $playlist->video_count . ' videos' . ($playlist->user ? ' • by ' . $playlist->user->name : '');
    }
    $plShareUrl = route('playlists.showByToken', $playlist->share_token);
    // Use the dedicated OG endpoint so previews always get a 1200x630 JPG under the
    // size/format limits of WhatsApp/Telegram/Discord/etc.
    $plOgImage = route('playlists.ogImage', $playlist);
@endphp
<meta property="og:title"            content="{{ $playlist->name }}">
<meta property="og:description"      content="{{ Str::limit(strip_tags($plDesc), 200) }}">
<meta property="og:image"            content="{{ $plOgImage }}">
<meta property="og:image:secure_url" content="{{ $plOgImage }}">
<meta property="og:image:type"       content="image/jpeg">
<meta property="og:image:width"      content="1200">
<meta property="og:image:height"     content="630">
<meta property="og:image:alt"        content="{{ $playlist->name }}">
<meta property="og:url"              content="{{ $plShareUrl }}">
<meta property="og:type"             content="music.playlist">
<meta property="og:site_name"        content="{{ config('app.name') }}">
<meta name="twitter:card"            content="summary_large_image">
<meta name="twitter:title"           content="{{ $playlist->name }}">
<meta name="twitter:description"     content="{{ Str::limit(strip_tags($plDesc), 200) }}">
<meta name="twitter:image"           content="{{ $plOgImage }}">
@endpush

@section('extra_styles')
<style>
/* ══════════════════════════════════════════════
   PLAYLIST PAGE
══════════════════════════════════════════════ */
.pl-page {
    max-width: 960px;
    margin: 0 auto;
    padding-bottom: 40px;
}

/* ── Hero header ── */
.pl-hero {
    position: relative;
    border-radius: 16px;
    overflow: hidden;
    margin-bottom: 24px;
    background: var(--bg-secondary);
}
.pl-hero-bg {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    filter: blur(28px) brightness(.35) saturate(1.4);
    transform: scale(1.08);
}
.pl-hero-content {
    position: relative;
    display: flex;
    gap: 28px;
    padding: 32px 28px;
    align-items: flex-end;
}

/* Thumbnail */
.pl-hero-thumb {
    flex-shrink: 0;
    width: 220px;
    aspect-ratio: 16/9;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 12px 40px rgba(0,0,0,.6);
    position: relative;
    background: #111;
}
.pl-hero-thumb img {
    width: 100%; height: 100%;
    object-fit: cover;
    display: block;
}
.pl-hero-count {
    position: absolute;
    inset: 0 0 0 auto;
    width: 56px;
    background: rgba(0,0,0,.75);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 4px; color: #fff;
    font-size: 11px; font-weight: 700;
    letter-spacing: .3px;
}
.pl-hero-count i { font-size: 20px; }

/* Info */
.pl-hero-info { flex: 1; min-width: 0; }
.pl-hero-type {
    font-size: 11px; font-weight: 700; letter-spacing: 1.5px;
    text-transform: uppercase; color: rgba(255,255,255,.55);
    margin-bottom: 8px;
}
.pl-hero-title {
    font-size: 26px; font-weight: 700;
    line-height: 1.2; margin-bottom: 10px;
    color: #fff;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.pl-hero-meta {
    font-size: 13px; color: rgba(255,255,255,.6);
    margin-bottom: 20px;
    display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
}
.pl-hero-meta .sep { opacity: .4; }
.pl-hero-desc {
    font-size: 13px; color: rgba(255,255,255,.55);
    margin-bottom: 20px; line-height: 1.5;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.pl-hero-badge {
    display: inline-flex; align-items: center; gap: 4px;
    background: rgba(255,255,255,.1); border-radius: 20px;
    padding: 2px 10px; font-size: 11px; font-weight: 600;
    color: rgba(255,255,255,.7);
}
.pl-hero-badge.private { color: #facc15; }

.pl-hero-actions {
    display: flex; gap: 10px; flex-wrap: wrap;
}

/* ── Video list ── */
.pl-list-card {
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    overflow: hidden;
}
.pl-list-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
}
.pl-list-header-title {
    font-size: 14px; font-weight: 700;
    color: var(--text-primary);
    display: flex; align-items: center; gap: 8px;
}
.pl-list-header-title i { color: var(--brand-red); }
.pl-list-count-badge {
    font-size: 12px; font-weight: 400;
    color: var(--text-secondary);
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 20px; padding: 2px 10px;
}

/* ── Video row ── */
.pl-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background .12s;
    position: relative;
    text-decoration: none; color: inherit;
}
.pl-item:last-child { border-bottom: none; }
.pl-item:hover { background: rgba(255,255,255,.04); }
.pl-item.is-playing { background: rgba(230,30,30,.07); }
.pl-item.is-playing::before {
    content: '';
    position: absolute; left: 0; top: 0; bottom: 0;
    width: 3px; background: var(--brand-red);
    border-radius: 0 2px 2px 0;
}

.pl-item-idx {
    width: 28px; text-align: center; flex-shrink: 0;
    font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums;
}
.pl-item.is-playing .pl-item-idx { color: var(--brand-red); font-weight: 700; }

.pl-item-drag {
    color: var(--text-secondary); cursor: grab;
    font-size: 15px; flex-shrink: 0; padding: 4px 2px;
    opacity: 0; transition: opacity .15s;
    display: flex; align-items: center;
}
.pl-item:hover .pl-item-drag { opacity: 1; }
.pl-item-drag:active { cursor: grabbing; }

.pl-item-thumb {
    width: 128px; height: 72px;
    border-radius: 6px; overflow: hidden;
    flex-shrink: 0; position: relative; background: #111;
    display: block;
}
.pl-item-thumb img {
    width: 100%; height: 100%;
    object-fit: cover; display: block;
    transition: transform .2s;
}
.pl-item:hover .pl-item-thumb img { transform: scale(1.03); }
.pl-item-dur {
    position: absolute; bottom: 3px; right: 4px;
    background: rgba(0,0,0,.85); color: #fff;
    font-size: 10px; font-weight: 600;
    padding: 1px 5px; border-radius: 3px;
    pointer-events: none;
}

.pl-item-body { flex: 1; min-width: 0; }
.pl-item-title {
    font-size: 13px; font-weight: 500;
    color: var(--text-primary);
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
    margin-bottom: 4px; line-height: 1.35;
}
.pl-item.is-playing .pl-item-title { color: var(--brand-red); }
.pl-item-meta {
    font-size: 12px; color: var(--text-secondary);
    display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.pl-now-playing {
    display: inline-flex; align-items: center; gap: 4px;
    font-size: 10px; font-weight: 700; color: var(--brand-red);
    text-transform: uppercase; letter-spacing: .5px;
}

.pl-item-actions { flex-shrink: 0; }
.pl-item-actions form { margin: 0; }

/* Sortable */
.pl-item.sortable-ghost  { opacity: .35; }
.pl-item.sortable-chosen { background: rgba(255,255,255,.05); }

/* ── Empty state ── */
.pl-empty {
    text-align: center; padding: 80px 24px;
    color: var(--text-secondary);
}
.pl-empty i { font-size: 64px; display: block; margin-bottom: 16px; opacity: .4; }
.pl-empty h2 { font-size: 18px; font-weight: 600; margin-bottom: 6px; color: var(--text-primary); }

/* ══ Mobile ══ */
@media (max-width: 768px) {
    .pl-hero-content {
        flex-direction: column;
        align-items: stretch;
        padding: 0 0 20px;
        gap: 0;
    }
    .pl-hero-bg { filter: blur(0) brightness(.4); transform: none; }
    .pl-hero-thumb {
        width: 100%; aspect-ratio: 16/9;
        border-radius: 0;
        box-shadow: none;
    }
    .pl-hero-info {
        padding: 16px 16px 0;
    }
    .pl-hero-title { font-size: 20px; }
    .pl-hero-actions { padding: 0 16px; }

    .pl-item-thumb { width: 100px; height: 56px; }
    .pl-item { padding: 8px 12px; gap: 10px; }
    .pl-item-title { font-size: 12px; }
    .pl-item-drag { display: none; }
    .pl-item-idx { width: 20px; font-size: 12px; }
}

@media (max-width: 480px) {
    .pl-item-thumb { width: 80px; height: 45px; }
    .pl-hero-title { font-size: 17px; }
}
</style>
@endsection

@section('content')
@php
    $canEdit    = $playlist->canEdit(Auth::user());
    $firstVideo = $videos->first();
    $firstUrl   = $firstVideo ? route('videos.show', $firstVideo) . '?playlist=' . $playlist->share_token : null;
    $thumbBg    = $playlist->thumbnail_url;
@endphp

<div class="pl-page">

    {{-- ══ Hero Header ══ --}}
    <div class="pl-hero">
        <div class="pl-hero-bg" style="background-image:url('{{ $thumbBg }}')"></div>
        <div class="pl-hero-content">

            {{-- Thumbnail --}}
            <div class="pl-hero-thumb">
                <img src="{{ $playlist->thumbnail_url }}" alt="{{ $playlist->name }}">
                <div class="pl-hero-count">
                    <i class="bi bi-collection-play-fill"></i>
                    {{ $videos->count() }}
                </div>
            </div>

            {{-- Info --}}
            <div class="pl-hero-info">
                <div class="pl-hero-type">Playlist</div>
                <h1 class="pl-hero-title">{{ $playlist->name }}</h1>

                <div class="pl-hero-meta">
                    @if($playlist->user)
                        <span>{{ $playlist->user->name }}</span>
                        <span class="sep">·</span>
                    @endif
                    <span>{{ $videos->count() }} {{ Str::plural('video', $videos->count()) }}</span>
                    @if($playlist->formatted_duration)
                        <span class="sep">·</span>
                        <span>{{ $playlist->formatted_duration }}</span>
                    @endif
                    <span class="sep">·</span>
                    <span><i class="bi bi-eye"></i> {{ number_format($playlist->view_count) }} {{ Str::plural('view', $playlist->view_count) }}</span>
                    @if($playlist->is_default)
                        <span class="pl-hero-badge" style="color:#60a5fa;">
                            <i class="bi bi-clock"></i> Watch Later
                        </span>
                    @elseif($playlist->visibility === 'private')
                        <span class="pl-hero-badge private">
                            <i class="bi bi-lock-fill"></i> Private
                        </span>
                    @elseif($playlist->visibility === 'unlisted')
                        <span class="pl-hero-badge" style="color:#a78bfa;">
                            <i class="bi bi-link-45deg"></i> Unlisted
                        </span>
                    @else
                        <span class="pl-hero-badge">
                            <i class="bi bi-globe"></i> Public
                        </span>
                    @endif
                </div>

                @if($playlist->description)
                    <div class="pl-hero-desc">{{ $playlist->description }}</div>
                @endif

                <div class="pl-hero-actions">
                    @if($firstUrl)
                        <a href="{{ $firstUrl }}" class="action-btn action-btn-primary">
                            <i class="bi bi-play-fill"></i> <span>Play All</span>
                        </a>
                        <button class="action-btn" onclick="playShuffled()">
                            <i class="bi bi-shuffle"></i> <span>Shuffle</span>
                        </button>
                    @endif
                    <button class="action-btn" onclick="openShareModal('{{ $playlist->share_url }}', '{{ addslashes($playlist->name) }}', '{{ route('playlists.recordShare', $playlist->id) }}')">
                        <i class="bi bi-share"></i> <span>Share</span>
                    </button>
                    @if($canEdit)
                        <button class="action-btn" onclick="openEditPlaylistModal()">
                            <i class="bi bi-pencil"></i> <span>Edit</span>
                        </button>
                    @endif
                </div>
            </div>

        </div>
    </div>

    {{-- ══ Video List ══ --}}
    @if($videos->isEmpty())
        <div class="pl-list-card">
            <div class="pl-empty">
                <i class="bi bi-collection-play"></i>
                <h2>No videos yet</h2>
                <p>Add videos to this playlist to start watching.</p>
            </div>
        </div>
    @else
        <div class="pl-list-card">
            <div class="pl-list-header">
                <span class="pl-list-header-title">
                    <i class="bi bi-collection-play-fill"></i>
                    {{ $playlist->name }}
                </span>
                <span class="pl-list-count-badge">{{ $videos->count() }} {{ Str::plural('video', $videos->count()) }}</span>
            </div>

            <div id="plVideoList">
                @foreach($videos as $i => $video)
                <div class="pl-item"
                     data-video-id="{{ $video->id }}"
                     data-show-url="{{ route('videos.show', $video) }}?playlist={{ $playlist->share_token }}">

                    @if($canEdit)
                        <i class="bi bi-grip-vertical pl-item-drag" onclick="event.stopPropagation()"></i>
                    @endif

                    <div class="pl-item-idx">{{ $i + 1 }}</div>

                    <a href="{{ route('videos.show', $video) }}?playlist={{ $playlist->share_token }}"
                       class="pl-item-thumb" onclick="event.stopPropagation()">
                        <img src="{{ $video->thumbnail_url }}" alt="{{ $video->title }}"
                             loading="{{ $i < 8 ? 'eager' : 'lazy' }}">
                        @if($video->duration)
                            <span class="pl-item-dur">{{ $video->formatted_duration }}</span>
                        @endif
                    </a>

                    <div class="pl-item-body">
                        <div class="pl-item-title">{{ $video->title }}</div>
                        <div class="pl-item-meta">
                            @if(optional($video->user)->name)
                                <span>{{ $video->user->name }}</span>
                            @endif
                            @if($video->view_count)
                                <span>·</span>
                                <span>{{ number_format($video->view_count) }} views</span>
                            @endif
                            @if($video->type && $video->type !== 'generic')
                                <span>·</span>
                                <span style="text-transform:capitalize;">{{ $video->type }}</span>
                            @endif
                        </div>
                    </div>

                    @if($canEdit)
                        <div class="pl-item-actions" onclick="event.stopPropagation()">
                            <form action="{{ route('playlists.removeVideo', [$playlist->id, $video->id]) }}" method="POST">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="action-btn icon-only danger" title="Remove from playlist">
                                    <i class="bi bi-x-lg"></i>
                                </button>
                            </form>
                        </div>
                    @endif
                </div>
                @endforeach
            </div>
        </div>
    @endif

</div>

{{-- ══ Edit Playlist Modal ══ --}}
@if($canEdit)
<div id="editPlaylistModal" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,.75); z-index:9999; align-items:center; justify-content:center; backdrop-filter:blur(4px);">
    <div style="background:var(--bg-secondary); border:1px solid var(--border-color); border-radius:14px; width:92%; max-width:480px; box-shadow:0 16px 48px rgba(0,0,0,.6); overflow:hidden;">
        <div style="display:flex; justify-content:space-between; align-items:center; padding:18px 22px; border-bottom:1px solid var(--border-color);">
            <h2 style="font-size:17px; font-weight:700; margin:0;">Edit Playlist</h2>
            <button type="button" id="closeEditPlaylistModalBtn"
                style="background:transparent; border:none; color:var(--text-secondary); cursor:pointer; font-size:20px; width:32px; height:32px; display:flex; align-items:center; justify-content:center; border-radius:50%; transition:background .15s;"
                onmouseover="this.style.background='var(--border-color)'" onmouseout="this.style.background='transparent'">
                <i class="bi bi-x-lg"></i>
            </button>
        </div>
        <div style="padding:22px;">
            {{-- Thumbnail --}}
            <div style="margin-bottom:18px;">
                <label style="display:block; margin-bottom:8px; font-weight:600; font-size:13px; color:var(--text-secondary); text-transform:uppercase; letter-spacing:.5px;">Thumbnail</label>
                <div id="editPlaylistThumbnailDropzone" style="border:2px dashed var(--border-color); border-radius:10px; padding:20px; text-align:center; cursor:pointer; background:var(--bg-dark); transition:border-color .2s;">
                    <input type="file" id="playlistThumbnailInput" accept="image/*" style="display:none;">
                    <div id="playlistThumbnailDefault" style="display:{{ ($playlist->thumbnail && !str_contains($playlist->thumbnail_url,'ui-avatars')) ? 'none' : 'block' }};">
                        <div style="font-size:32px; color:var(--text-secondary); margin-bottom:8px; opacity:.5;"><i class="bi bi-card-image"></i></div>
                        <p style="color:var(--text-secondary); font-size:13px; margin:0 0 3px;">Click or drag to upload</p>
                        <p style="color:var(--text-secondary); font-size:11px; margin:0; opacity:.5;">JPG, PNG, WebP · max 20 MB</p>
                    </div>
                    <div id="playlistThumbnailPreviewWrap" style="display:{{ ($playlist->thumbnail && !str_contains($playlist->thumbnail_url,'ui-avatars')) ? 'inline-block' : 'none' }}; position:relative;">
                        <img id="playlistThumbnailPreview" src="{{ ($playlist->thumbnail && !str_contains($playlist->thumbnail_url,'ui-avatars')) ? $playlist->thumbnail_url : '' }}" alt="" style="max-width:100%; max-height:150px; border-radius:8px; object-fit:cover;">
                        <button type="button" onclick="removePlaylistThumbnail(event)" style="position:absolute; top:-8px; right:-8px; width:22px; height:22px; background:var(--brand-red); color:white; border:none; border-radius:50%; cursor:pointer; display:flex; align-items:center; justify-content:center; font-size:12px;"><i class="bi bi-x"></i></button>
                    </div>
                </div>
            </div>
            {{-- Name --}}
            <div style="margin-bottom:16px;">
                <label style="display:block; margin-bottom:7px; font-weight:600; font-size:13px; color:var(--text-secondary); text-transform:uppercase; letter-spacing:.5px;">Name *</label>
                <input type="text" id="editPlName" value="{{ $playlist->name }}" required
                    style="width:100%; padding:11px 14px; border:1px solid var(--border-color); border-radius:8px; background:var(--bg-dark); color:var(--text-primary); font-size:14px; outline:none; box-sizing:border-box; transition:border-color .15s;"
                    onfocus="this.style.borderColor='var(--brand-red)'" onblur="this.style.borderColor='var(--border-color)'">
            </div>
            {{-- Description --}}
            <div style="margin-bottom:16px;">
                <label style="display:block; margin-bottom:7px; font-weight:600; font-size:13px; color:var(--text-secondary); text-transform:uppercase; letter-spacing:.5px;">Description</label>
                <textarea id="editPlDesc" rows="3"
                    style="width:100%; padding:11px 14px; border:1px solid var(--border-color); border-radius:8px; background:var(--bg-dark); color:var(--text-primary); font-size:14px; outline:none; resize:none; box-sizing:border-box; transition:border-color .15s;"
                    onfocus="this.style.borderColor='var(--brand-red)'" onblur="this.style.borderColor='var(--border-color)'">{{ $playlist->description }}</textarea>
            </div>
            {{-- Visibility --}}
            <div style="margin-bottom:22px;">
                <label style="display:block; margin-bottom:10px; font-weight:600; font-size:13px; color:var(--text-secondary); text-transform:uppercase; letter-spacing:.5px;">Visibility</label>
                <div style="display:flex; flex-direction:column; gap:8px;">
                    <label style="display:flex; align-items:center; gap:12px; cursor:pointer; padding:10px 14px; border-radius:8px; background:var(--bg-dark); border:1px solid var(--border-color);">
                        <input type="radio" name="editPlVisibility" id="editPlVisibility" value="public" {{ $playlist->visibility === 'public' ? 'checked' : '' }}
                            style="accent-color:var(--brand-red); cursor:pointer; width:16px; height:16px; flex-shrink:0;">
                        <div>
                            <div style="display:flex; align-items:center; gap:6px; font-size:14px; font-weight:500;"><i class="bi bi-globe"></i> Public</div>
                            <div style="color:var(--text-secondary); font-size:12px; margin-top:2px;">Anyone can search for and view this playlist</div>
                        </div>
                    </label>
                    <label style="display:flex; align-items:center; gap:12px; cursor:pointer; padding:10px 14px; border-radius:8px; background:var(--bg-dark); border:1px solid var(--border-color);">
                        <input type="radio" name="editPlVisibility" value="unlisted" {{ $playlist->visibility === 'unlisted' ? 'checked' : '' }}
                            style="accent-color:var(--brand-red); cursor:pointer; width:16px; height:16px; flex-shrink:0;">
                        <div>
                            <div style="display:flex; align-items:center; gap:6px; font-size:14px; font-weight:500;"><i class="bi bi-link-45deg"></i> Unlisted</div>
                            <div style="color:var(--text-secondary); font-size:12px; margin-top:2px;">Anyone with the link can view, but it won't appear in search</div>
                        </div>
                    </label>
                    <label style="display:flex; align-items:center; gap:12px; cursor:pointer; padding:10px 14px; border-radius:8px; background:var(--bg-dark); border:1px solid var(--border-color);">
                        <input type="radio" name="editPlVisibility" value="private" {{ $playlist->visibility === 'private' ? 'checked' : '' }}
                            style="accent-color:var(--brand-red); cursor:pointer; width:16px; height:16px; flex-shrink:0;">
                        <div>
                            <div style="display:flex; align-items:center; gap:6px; font-size:14px; font-weight:500;"><i class="bi bi-lock-fill"></i> Private</div>
                            <div style="color:var(--text-secondary); font-size:12px; margin-top:2px;">Only you can view this playlist</div>
                        </div>
                    </label>
                </div>
            </div>
            {{-- Actions --}}
            <div style="display:flex; gap:10px; justify-content:space-between; align-items:center;">
                @if(!$playlist->is_default)
                    <button type="button" onclick="deletePlaylist()" class="action-btn action-btn-danger">
                        <i class="bi bi-trash"></i> <span>Delete</span>
                    </button>
                @else
                    <div></div>
                @endif
                <div style="display:flex; gap:10px;">
                    <button type="button" onclick="closeEditPlaylistModal()" class="action-btn">Cancel</button>
                    <button type="button" onclick="savePlaylist()" class="action-btn action-btn-primary">
                        <i class="bi bi-check-lg"></i> <span>Save</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
@endif
@endsection

@section('scripts')
@php
$plVideosJson = $videos->map(fn($v) => [
    'id'      => $v->id,
    'showUrl' => route('videos.show', $v) . '?playlist=' . $playlist->share_token,
])->values()->all();
@endphp
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.0/Sortable.min.js"></script>
<script>
const PLAYLIST_ID     = {{ $playlist->id }};
const PLAYLIST_VIDEOS = @json($plVideosJson);

function playShuffled() {
    if (!PLAYLIST_VIDEOS.length) return;
    const order = [...Array(PLAYLIST_VIDEOS.length).keys()];
    for (let i = order.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [order[i], order[j]] = [order[j], order[i]];
    }
    localStorage.setItem('ytpShuffledOrder_' + PLAYLIST_ID, JSON.stringify(order));
    localStorage.setItem('ytpShuffleOn_' + PLAYLIST_ID, '1');
    window.location.href = PLAYLIST_VIDEOS[order[0]].showUrl;
}

document.querySelectorAll('.pl-item').forEach(el => {
    el.addEventListener('click', () => {
        const url = el.dataset.showUrl;
        if (url) window.location.href = url;
    });
});

document.addEventListener('DOMContentLoaded', function () {
    const list = document.getElementById('plVideoList');
    if (list && typeof Sortable !== 'undefined' && {{ $canEdit ? 'true' : 'false' }}) {
        new Sortable(list, {
            handle: '.pl-item-drag',
            animation: 150,
            ghostClass: 'sortable-ghost',
            chosenClass: 'sortable-chosen',
            onEnd() {
                const ids = Array.from(list.querySelectorAll('.pl-item'))
                    .map(el => parseInt(el.dataset.videoId));
                list.querySelectorAll('.pl-item-idx').forEach((el, i) => el.textContent = i + 1);
                fetch('{{ route("playlists.reorder", $playlist->id) }}', {
                    method: 'PUT',
                    headers: {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}',
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                    },
                    body: JSON.stringify({ video_ids: ids }),
                }).then(r => r.json()).then(d => {
                    if (!d.success) showToast('Failed to save order', 'error');
                });
            },
        });
    }

    const dropzone   = document.getElementById('editPlaylistThumbnailDropzone');
    const thumbInput = document.getElementById('playlistThumbnailInput');
    if (dropzone && thumbInput) {
        dropzone.addEventListener('click', e => {
            if (e.target.closest('button')) return;
            if (typeof window.openCropperModal_thumb_pl_edit === 'function') {
                window.openCropperModal_thumb_pl_edit();
            } else {
                thumbInput.click();
            }
        });
        thumbInput.addEventListener('change', function () { handleThumbUpload(this); });
        dropzone.addEventListener('dragover',  e => { e.preventDefault(); dropzone.style.borderColor = 'var(--brand-red)'; });
        dropzone.addEventListener('dragleave', ()  => { dropzone.style.borderColor = 'var(--border-color)'; });
        dropzone.addEventListener('drop', e => {
            e.preventDefault(); dropzone.style.borderColor = 'var(--border-color)';
            if (e.dataTransfer.files.length) {
                const droppedFile = e.dataTransfer.files[0];
                if (typeof window.tcPreload_thumb_pl_edit === 'function') {
                    window.tcPreload_thumb_pl_edit(droppedFile);
                    window.openCropperModal_thumb_pl_edit();
                } else {
                    thumbInput.files = e.dataTransfer.files;
                    handleThumbUpload(thumbInput);
                }
            }
        });
    }
    const closeBtn = document.getElementById('closeEditPlaylistModalBtn');
    if (closeBtn) closeBtn.addEventListener('click', closeEditPlaylistModal);
    const modal = document.getElementById('editPlaylistModal');
    if (modal) modal.addEventListener('click', e => { if (e.target === modal) closeEditPlaylistModal(); });
    document.addEventListener('keydown', e => { if (e.key === 'Escape') closeEditPlaylistModal(); });
});

function openEditPlaylistModal()  {
    // Reset transient state every time the modal opens so flags from a previous
    // open (e.g. the user clicked X then closed without saving) don't leak.
    window._removePlThumb = false;
    const fi = document.getElementById('playlistThumbnailInput');
    if (fi) fi.value = '';
    const m = document.getElementById('editPlaylistModal');
    if (m) m.style.display = 'flex';
}
// Auto-open the edit modal when arriving with #edit (e.g. from playlist-card menu).
@if($canEdit ?? false)
if (window.location.hash === '#edit') {
    document.addEventListener('DOMContentLoaded', function () { openEditPlaylistModal(); });
}
@endif
function closeEditPlaylistModal() { const m = document.getElementById('editPlaylistModal'); if (m) m.style.display = 'none'; }

function handleThumbUpload(input) {
    if (!input.files?.[0]) return;
    const file = input.files[0];
    if (!file.type.startsWith('image/')) { showToast('Please select an image file', 'error'); return; }
    if (file.size > 20 * 1024 * 1024) { showToast('Image must be under 20 MB', 'error'); return; }
    // Picking a new file cancels any pending "remove" intent — otherwise the
    // controller would receive both `thumbnail` and `remove_thumbnail=1`.
    window._removePlThumb = false;
    const reader = new FileReader();
    reader.onload = e => {
        document.getElementById('playlistThumbnailPreview').src = e.target.result;
        document.getElementById('playlistThumbnailPreviewWrap').style.display = 'inline-block';
        document.getElementById('playlistThumbnailDefault').style.display = 'none';
    };
    reader.readAsDataURL(file);
}

function removePlaylistThumbnail(e) {
    e.preventDefault(); e.stopPropagation();
    document.getElementById('playlistThumbnailInput').value = '';
    document.getElementById('playlistThumbnailPreview').src = '';
    document.getElementById('playlistThumbnailPreviewWrap').style.display = 'none';
    document.getElementById('playlistThumbnailDefault').style.display = 'block';
    window._removePlThumb = true;
}

function savePlaylist() {
    const name = document.getElementById('editPlName')?.value?.trim();
    if (!name) { showToast('Name is required', 'error'); return; }
    const fd = new FormData();
    fd.append('_method',      'PUT');
    fd.append('_token',       '{{ csrf_token() }}');
    fd.append('name',         name);
    fd.append('description',  document.getElementById('editPlDesc')?.value ?? '');
    fd.append('visibility',   document.querySelector('input[name="editPlVisibility"]:checked')?.value ?? 'private');
    const thumb = document.getElementById('playlistThumbnailInput');
    if (thumb?.files?.[0]) fd.append('thumbnail', thumb.files[0]);
    if (window._removePlThumb) fd.append('remove_thumbnail', '1');
    fetch('{{ route("playlists.update", $playlist->id) }}', {
        method: 'POST', body: fd,
        headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}', 'Accept': 'application/json' },
    }).then(r => r.json()).then(d => {
        if (d.success) { showToast('Playlist updated!', 'success'); closeEditPlaylistModal(); location.reload(); }
        else showToast(d.message || 'Failed to update', 'error');
    }).catch(() => showToast('Failed to update playlist', 'error'));
}

function deletePlaylist() {
    showConfirm('Delete this playlist?', function () {
        fetch('{{ route("playlists.destroy", $playlist->id) }}', {
            method: 'DELETE',
            headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}', 'Accept': 'application/json' },
        }).then(r => r.json()).then(d => {
            if (d.success) window.location.href = '{{ route("playlists.index") }}';
            else showToast('Failed to delete', 'error');
        });
    }, 'Delete');
}
</script>
<x-image-cropper
    id="thumb_pl_edit"
    :width="448"
    :height="252"
    shape="square"
    target-input="playlistThumbnailInput"
    preview-img="playlistThumbnailPreview"
    output-width="1280"
    title="Crop Playlist Thumbnail"
/>
@endsection
