@extends('layouts.app')

@section('title', isset($query) ? 'Search: ' . $query . ' | ' . config('app.name') : 'Video Gallery | ' .
    config('app.name'))

@section('extra_styles')
    <style>
        /* Search info */
        .search-info {
            margin-bottom: 20px;
            padding: 16px;
            background: var(--bg-secondary);
            border-radius: 12px;
        }

        .search-info h2 {
            font-size: 20px;
            margin: 0;
        }

        .search-info p {
            color: var(--text-secondary);
            margin: 8px 0 0;
        }

        /* Video Grid */
        .yt-video-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 24px;
        }

        .yt-video-card {
            cursor: pointer;
        }

        .yt-video-thumb {
            position: relative;
            aspect-ratio: 16/9;
            border-radius: 12px;
            overflow: hidden;
            background: #1a1a1a;
        }

        .yt-video-thumb img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            position: absolute;
            top: 0;
            left: 0;
        }

        .yt-video-thumb video {
            width: 100%;
            height: 100%;
            object-fit: contain;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            transition: opacity 0.3s ease;
            background: #000;
        }

        .yt-video-thumb video.active {
            opacity: 1;
        }

        .yt-video-duration {
            position: absolute;
            bottom: 8px;
            right: 8px;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 3px 6px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
        }

        .yt-video-info {
            display: flex;
            margin-top: 12px;
            gap: 12px;
        }

        .yt-channel-icon {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: #555;
            flex-shrink: 0;
        }

        .yt-video-details {
            flex: 1;
        }

        .yt-video-title {
            font-size: 16px;
            font-weight: 500;
            color: var(--text-primary);
            margin: 0 0 4px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            line-height: 1.3;
        }

        .yt-video-title a {
            color: inherit;
            text-decoration: none;
        }

        .yt-channel-name,
        .yt-video-meta {
            color: var(--text-secondary);
            font-size: 14px;
        }

        /* More button */
        .yt-more-btn {
            background: transparent;
            border: none;
            color: var(--text-primary);
            cursor: pointer;
            padding: 4px;
            border-radius: 50%;
        }

        .yt-more-dropdown {
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            padding: 8px 0;
            min-width: 200px;
        }

        .yt-more-dropdown-item {
            display: flex;
            align-items: center;
            gap: 16px;
            padding: 10px 16px;
            color: var(--text-primary);
            text-decoration: none;
            cursor: pointer;
            background: transparent;
            border: none;
            width: 100%;
            text-align: left;
            font-size: 14px;
        }

        .yt-more-dropdown-item:hover {
            background: var(--border-color);
        }

        /* Empty State */
        .yt-empty {
            text-align: center;
            padding: 80px 20px;
        }

        .yt-empty-icon {
            font-size: 80px;
            color: var(--text-secondary);
        }

        .yt-empty-title {
            font-size: 24px;
            margin: 20px 0 8px;
        }

        .yt-empty-text {
            color: var(--text-secondary);
            margin-bottom: 20px;
        }

        /* Responsive */
        @media (max-width: 1200px) {
            .yt-video-grid {
                grid-template-columns: repeat(3, 1fr);
                gap: 20px;
            }
        }

        @media (max-width: 992px) {
            .yt-video-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 576px) {
            .yt-video-grid {
                grid-template-columns: 1fr;
            }

            .yt-header-right .yt-icon-btn:not(:last-child) {
                display: none;
            }
        }
    </style>
@endsection

@section('content')
    @isset($query)
        <div class="search-info">
            <h2>Search results for "{{ $query }}"</h2>
            <p>{{ $videos->total() }} videos found</p>
        </div>
        @endif

        @if ($videos->isEmpty())
            <div class="yt-empty">
                <i class="bi bi-camera-video yt-empty-icon"></i>
                @isset($query)
                    <h2 class="yt-empty-title">No results found</h2>
                    <p class="yt-empty-text">Try different keywords or browse all videos.</p>
                @else
                    <h2 class="yt-empty-title">No videos yet</h2>
                    <p class="yt-empty-text">Be the first to upload a video!</p>
                @endisset
                @auth
                    <a href="/videos/create" class="yt-upload-btn" style="display: inline-flex;">
                        <i class="bi bi-plus-lg"></i>
                        Upload Video
                    </a>
                @else
                    <a href="{{ route('login') }}" class="yt-upload-btn" style="display: inline-flex;">
                        <i class="bi bi-box-arrow-in-right"></i>
                        Login to Upload
                    </a>
                @endauth
            </div>
        @else
            <div class="yt-video-grid">
                @foreach ($videos as $video)
                    <x-video-card :video="$video" />
                @endforeach
            </div>

        @endif

        @include('layouts.partials.share-modal')
    @endsection

@section('scripts')
    <script>
        function playVideo(card) {
            const video = card.querySelector('video');
            if (video) {
                video.currentTime = 0;
                video.volume = 0.10; // Set volume to 10%
                video.play().catch(function(e) {
                    // Auto-play may be blocked, ignore error
                });
                video.classList.add('active');
            }
        }

        function stopVideo(card) {
            const video = card.querySelector('video');
            if (video) {
                video.pause();
                video.currentTime = 0;
                video.classList.remove('active');
            }
        }

        // Mobile touch support for hover-like behavior
        document.addEventListener('touchstart', function(e) {
            const card = e.target.closest('.yt-video-card');
            if (card) {
                // Stop any other playing videos first
                document.querySelectorAll('.yt-video-card').forEach(function(otherCard) {
                    if (otherCard !== card) {
                        stopVideo(otherCard);
                    }
                });
                playVideo(card);
            }
        }, {
            passive: true
        });

        document.addEventListener('touchend', function(e) {
            const card = e.target.closest('.yt-video-card');
            if (card) {
                stopVideo(card);
            }
        }, {
            passive: true
        });
    </script>
@endsection


<!-- Extra Mobile Styles -->
<style>
    @media (max-width: 400px) {
        .yt-video-grid {
            gap: 12px;
        }

        .yt-video-thumb {
            border-radius: 8px;
        }

        .yt-video-info {
            gap: 8px;
            margin-top: 8px;
        }

        .yt-channel-icon {
            width: 28px;
            height: 28px;
        }

        .yt-video-title {
            font-size: 13px;
        }

        .yt-channel-name,
        .yt-video-meta {
            font-size: 11px;
        }

        .search-info {
            padding: 12px;
        }

        .search-info h2 {
            font-size: 16px;
        }
    }
</style>

