<!-- Add to Playlist Modal -->
<div id="addToPlaylistModal" class="playlist-modal-overlay"
    style="display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.7); z-index: 9999; align-items: center; justify-content: center;">
    <div class="playlist-modal-content"
        style="background: #282828; border-radius: 12px; width: 90%; max-width: 380px; max-height: 85vh; display: flex; flex-direction: column; box-shadow: 0 8px 32px rgba(0,0,0,0.5); overflow: hidden;">
        <!-- Header -->
        <div class="playlist-modal-header"
            style="display: flex; justify-content: space-between; align-items: center; padding: 20px 24px; border-bottom: 1px solid #3f3f3f;">
            <h2 style="font-size: 18px; font-weight: 600; margin: 0; color: #fff;">Save to playlist</h2>
            <button type="button" id="closePlaylistModalBtn"
                style="background: transparent; border: none; color: #aaa; cursor: pointer; font-size: 24px; padding: 4px; line-height: 1; border-radius: 50%; width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; transition: all 0.2s;">
                <i class="bi bi-x-lg"></i>
            </button>
        </div>

        <!-- Body -->
        <div style="padding: 16px 24px; flex: 1; overflow-y: auto;">
            <!-- Create New Playlist Option -->
            @auth
                <div style="margin-bottom: 16px;">
                    <button onclick="showCreatePlaylistInModal()" class="create-playlist-btn"
                        style="width: 100%; display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: transparent; border: none; color: #fff; cursor: pointer; border-radius: 8px; transition: background 0.2s; font-size: 14px;">
                        <span
                            style="width: 36px; height: 36px; background: #3f3f3f; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                            <i class="bi bi-plus-lg" style="font-size: 18px;"></i>
                        </span>
                        <span style="font-weight: 500;">Create new playlist</span>
                    </button>
                </div>

                <!-- Create Playlist Form (Hidden by default) -->
                <div id="createPlaylistInModal"
                    style="display: none; margin-bottom: 16px; padding: 16px; background: #1f1f1f; border-radius: 10px; border: 1px solid #3f3f3f;">
                    <input type="text" id="newPlaylistName" placeholder="Playlist name"
                        style="width: 100%; padding: 12px 14px; margin-bottom: 12px; border: 1px solid #3f3f3f; border-radius: 8px; background: #121212; color: #fff; font-size: 14px; outline: none; transition: border-color 0.2s;">
                    <div style="display: flex; gap: 10px; justify-content: flex-end;">
                        <button onclick="hideCreatePlaylistInModal()" class="action-btn">Cancel</button>
                        <button onclick="createPlaylistFromModal()" class="action-btn action-btn-primary">
                            <i class="bi bi-plus-lg"></i> <span>Create</span>
                        </button>
                    </div>
                </div>
            @endauth

            <!-- Playlist List Container -->
            <div id="playlistListContainer" style="flex: 1; overflow-y: auto; min-height: 80px;">
                <!-- Playlist items will be loaded here via JavaScript -->
            </div>
        </div>

        <!-- Footer -->
        <div style="padding: 16px 24px; border-top: 1px solid #3f3f3f;">
            <a href="{{ route('playlists.index') }}"
                style="display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-size: 14px; padding: 10px 12px; border-radius: 8px; transition: background 0.2s;">
                <i class="bi bi-collection-play" style="font-size: 18px;"></i>
                <span style="font-weight: 500;">View all playlists</span>
            </a>
        </div>
    </div>
</div>

<style>
    /* Modal Overlay with proper centering */
    .playlist-modal-overlay {
        backdrop-filter: blur(2px);
    }

    /* Modal Content */
    .playlist-modal-content {
        font-family: "Roboto", "Arial", sans-serif;
    }

    /* Header button hover */
    .playlist-modal-header button:hover {
        background: #3f3f3f !important;
        color: #fff !important;
    }

    /* Create playlist button hover */
    .create-playlist-btn:hover {
        background: #3f3f3f !important;
    }

    /* Input focus */
    #newPlaylistName:focus {
        border-color: #e61e1e !important;
    }

    /* Button hover effects */
    #createPlaylistInModal button:hover {
        opacity: 0.9;
    }

    #createPlaylistInModal button:last-child:hover {
        background: #cc1a1a !important;
    }

    /* Footer link hover */
    .playlist-modal-footer a:hover {
        background: #3f3f3f;
    }

    /* Playlist items */
    .playlist-item {
        border-radius: 8px;
    }

    .playlist-item:hover {
        background: #3f3f3f !important;
    }

    /* Scrollbar styling */
    #playlistListContainer::-webkit-scrollbar {
        width: 8px;
    }

    #playlistListContainer::-webkit-scrollbar-track {
        background: transparent;
    }

    #playlistListContainer::-webkit-scrollbar-thumb {
        background: #555;
        border-radius: 4px;
    }

    #playlistListContainer::-webkit-scrollbar-thumb:hover {
        background: #666;
    }

    /* Mobile centering fix */
    @media (max-width: 576px) {
        body.modal-open {
            overflow: hidden !important;
        }

        .playlist-modal-overlay {
            min-height: 100vh !important;
            height: 100dvh !important;
            padding: 70px 0 80px 0 !important;
            box-sizing: border-box !important;
            width: 100vw !important;
            left: 0 !important;
        }

        .playlist-modal-content {
            max-height: 90dvh !important;
            margin: 0 auto !important;
            width: 95vw !important;
            max-width: 95vw !important;
            border-radius: 16px !important;
        }
    }
</style>

<script>
    let currentModalVideoId = null;

    // Check if user is authenticated
    function isAuthenticated() {
        return {{ auth()->check() ? 'true' : 'false' }};
    }

    // Close button event listener
    document.addEventListener('DOMContentLoaded', function() {
        const closeBtn = document.getElementById('closePlaylistModalBtn');
        if (closeBtn) {
            closeBtn.addEventListener('click', function(e) {
                e.stopPropagation();
                closeAddToPlaylistModal();
            });
        }
    });

    function openAddToPlaylistModal(videoId) {
        currentModalVideoId = videoId;

        // Close any open dropdown menus first
        const activeDropdowns = document.querySelectorAll('.dropdown-menu.show');
        activeDropdowns.forEach(function(dropdown) {
            dropdown.classList.remove('show');
        });

        // Also close Bootstrap dropdowns
        const dropdownToggles = document.querySelectorAll('.dropdown-toggle[aria-expanded="true"]');
        dropdownToggles.forEach(function(toggle) {
            toggle.click();
        });

        document.body.classList.add('modal-open');

        const modal = document.getElementById('addToPlaylistModal');
        modal.style.display = 'flex';

        document.getElementById('createPlaylistInModal').style.display = 'none';

        // Load playlists when modal opens
        loadPlaylistsForModal(videoId);
    }

    function closeAddToPlaylistModal() {
        const modal = document.getElementById('addToPlaylistModal');
        modal.style.display = 'none';
        document.body.classList.remove('modal-open');
        currentModalVideoId = null;
    }

    // Close modal when clicking outside
    document.getElementById('addToPlaylistModal').addEventListener('click', function(e) {
        if (e.target === this) {
            closeAddToPlaylistModal();
        }
    });

    // Close on escape key
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            const modal = document.getElementById('addToPlaylistModal');
            if (modal && modal.style.display === 'flex') {
                closeAddToPlaylistModal();
            }
        }
    });

    // Load playlists for modal
    function loadPlaylistsForModal(videoId) {
        const container = document.getElementById('playlistListContainer');

        @auth
        // Fetch playlists data
        fetch('{{ route('playlists.userPlaylists') }}', {
                headers: {
                    'Accept': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success && data.playlists && data.playlists.length > 0) {
                    let html = '';
                    data.playlists.forEach(function(playlist) {
                        const isInPlaylist = playlist.video_ids && playlist.video_ids.includes(parseInt(
                            videoId));
                        const visibilityText = playlist.visibility === 'public' ? 'Public' : 'Private';
                        const durationText = playlist.formatted_duration || '0m';
                        html += `
                    <div class="playlist-item" style="display: flex; align-items: flex-start; justify-content: space-between; padding: 12px; border-radius: 8px; cursor: pointer; transition: background 0.2s; margin-bottom: 8px;"
                        onmouseover="this.style.background='#3f3f3f'"
                        onmouseout="this.style.background='transparent'"
                        onclick="toggleVideoInPlaylist(${playlist.id}, ${videoId}, ${isInPlaylist ? 'true' : 'false'})">
                        <div style="display: flex; align-items: flex-start; gap: 12px; flex: 1; min-width: 0;">
                            <div style="width: 100px; height: 56px; background: #1a1a1a; border-radius: 8px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; overflow: hidden; position: relative;">
                                ${playlist.thumbnail_url
                                    ? `<img src="${playlist.thumbnail_url}" style="width: 100%; height: 100%; object-fit: cover;">`
                                    : `<i class="bi bi-collection-play" style="font-size: 24px; color: #666;"></i>`
                                }
                                ${playlist.is_default
                                    ? `<span style="position: absolute; top: 2px; left: 2px; background: rgba(230,30,30,0.9); color: white; padding: 1px 4px; border-radius: 2px; font-size: 9px; font-weight: 600;">WL</span>`
                                    : ''
                                }
                            </div>
                            <div style="min-width: 0; flex: 1;">
                                <div style="font-weight: 500; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #fff;">${playlist.name}</div>
                                <div style="font-size: 12px; color: #aaa; margin-top: 2px;">
                                    ${playlist.video_count || 0} videos • ${durationText}
                                </div>
                                <div style="font-size: 11px; color: #777; margin-top: 2px;">
                                    ${visibilityText}
                                </div>
                                ${playlist.description ? `<div style="font-size: 11px; color: #777; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">${playlist.description}</div>` : ''}
                            </div>
                        </div>
                        <div style="flex-shrink: 0; margin-left: 8px;">
                            ${isInPlaylist
                                ? '<i class="bi bi-check-circle-fill" style="color: #e61e1e; font-size: 20px;"></i>'
                                : '<i class="bi bi-plus-circle" style="color: #aaa; font-size: 20px;"></i>'}
                        </div>
                    </div>
                `;
                    });
                    container.innerHTML = html;
                } else {
                    container.innerHTML = `
                <div style="text-align: center; color: #aaa; padding: 30px 20px;">
                    <i class="bi bi-collection-play" style="font-size: 36px; margin-bottom: 12px; display: block; color: #555;"></i>
                    <p style="margin: 0 0 8px; font-size: 14px; color: #fff;">No playlists yet</p>
                    <p style="margin: 0; font-size: 12px;">Create a playlist to save videos</p>
                </div>
            `;
                }
            })
            .catch(error => {
                console.error('Error loading playlists:', error);
                container.innerHTML = `
            <div style="text-align: center; color: #aaa; padding: 30px 20px;">
                <i class="bi bi-exclamation-circle" style="font-size: 28px; margin-bottom: 12px; display: block; color: #ef4444;"></i>
                <p style="margin: 0; font-size: 14px; color: #fff;">Failed to load playlists</p>
            </div>
        `;
            });
    @else
        container.innerHTML = `
        <div style="text-align: center; color: #aaa; padding: 30px 20px;">
            <i class="bi bi-person-circle" style="font-size: 40px; margin-bottom: 12px; display: block; color: #555;"></i>
            <p style="margin: 0 0 8px; font-size: 14px; color: #fff;">Sign in to save videos to playlists</p>
            <p style="margin: 0 0 16px; font-size: 12px;">Create a playlist to organize your videos</p>
            <a href="{{ route('login') }}?redirect={{ urlencode(request()->fullUrl()) }}"
               style="display: inline-block; padding: 10px 24px; background: #e61e1e; color: white; border-radius: 20px; text-decoration: none; font-weight: 500; font-size: 14px; transition: background 0.2s;">
                Sign In
            </a>
        </div>
    `;
    @endauth
    }

    // Toggle video in/out of a playlist. `currentlyIn` reflects what the row
    // showed when the user clicked, so a single click flips the state via the
    // matching REST endpoint (POST to add, DELETE to remove).
    function toggleVideoInPlaylist(playlistId, videoId, currentlyIn) {
        if (!videoId) return;

        if (!isAuthenticated()) {
            window.location.href = '{{ route('login') }}?redirect=' + encodeURIComponent(window.location.href);
            return;
        }

        // Both add and remove go through the same /playlists/{id}/videos URL with
        // a JSON body carrying the numeric video id — keeps the front-end ignorant
        // of the encoded route key used by GET /videos/{video}.
        var url    = `/playlists/${playlistId}/videos`;
        var method = currentlyIn ? 'DELETE' : 'POST';
        var body   = JSON.stringify({ video_id: videoId });

        fetch(url, {
                method,
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Accept': 'application/json'
                },
                body
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showToast(data.message || (currentlyIn ? 'Removed from playlist' : 'Added to playlist'));
                    loadPlaylistsForModal(videoId); // refresh checkmarks
                }
            })
            .catch(error => console.error('Error:', error));
    }

    // Show create playlist form in modal
    function showCreatePlaylistInModal() {
        // Check authentication before creating playlist
        if (!isAuthenticated()) {
            window.location.href = '{{ route('login') }}?redirect=' + encodeURIComponent(window.location.href);
            return;
        }
        document.getElementById('createPlaylistInModal').style.display = 'block';
        document.getElementById('newPlaylistName').focus();
    }

    // Hide create playlist form
    function hideCreatePlaylistInModal() {
        document.getElementById('createPlaylistInModal').style.display = 'none';
        document.getElementById('newPlaylistName').value = '';
    }

    // Create playlist from modal
    function createPlaylistFromModal() {
        const name = document.getElementById('newPlaylistName').value.trim();
        if (!name) {
            showToast('Please enter a playlist name', 'error');
            return;
        }

        fetch('{{ route('playlists.store') }}', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Accept': 'application/json'
                },
                body: JSON.stringify({
                    name: name,
                    visibility: 'private'
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    hideCreatePlaylistInModal();
                    showToast('Playlist created!');
                    // Reload playlists to show new playlist
                    loadPlaylistsForModal(currentModalVideoId);
                }
            })
            .catch(error => console.error('Error:', error));
    }

    // Simple toast notification
    function showToast(message) {
        // Remove existing toast if any
        const existing = document.querySelector('.playlist-toast');
        if (existing) existing.remove();

        const toast = document.createElement('div');
        toast.className = 'playlist-toast';
        toast.style.cssText = `
        position: fixed;
        bottom: 80px;
        left: 50%;
        transform: translateX(-50%);
        background: #fff;
        color: #0f0f0f;
        padding: 14px 28px;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 500;
        z-index: 10000;
        animation: fadeInUp 0.3s ease;
        box-shadow: 0 4px 20px rgba(0,0,0,0.4);
        max-width: 90%;
        text-align: center;
    `;
        toast.textContent = message;
        document.body.appendChild(toast);

        setTimeout(() => {
            toast.style.animation = 'fadeOutDown 0.3s ease';
            setTimeout(() => toast.remove(), 300);
        }, 2500);
    }
</script>

<style>
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateX(-50%) translateY(20px);
        }

        to {
            opacity: 1;
            transform: translateX(-50%) translateY(0);
        }
    }

    @keyframes fadeOutDown {
        from {
            opacity: 1;
            transform: translateX(-50%) translateY(0);
        }

        to {
            opacity: 0;
            transform: translateX(-50%) translateY(20px);
        }
    }
</style>
