@extends(getTemplate().'.layouts.app')

@push('styles_top')
    <style>
        .graduate-video-card {
            background: #fff;
            border: 1px solid #e8e8e8;
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 24px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
            transition: box-shadow 0.2s ease, transform 0.2s ease;
        }
        .graduate-video-card:hover {
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
            transform: translateY(-2px);
        }
        .graduate-video-card .gv-media-box {
            position: relative;
            aspect-ratio: 16 / 9;
            background: #000;
            overflow: hidden;
        }
        .graduate-video-card .gv-media-box video,
        .graduate-video-card .gv-media-box .embed-responsive,
        .graduate-video-card .gv-media-box .video-facade {
            position: absolute;
            inset: 0;
            width: 100%;
            height: 100%;
        }
        .graduate-video-card .gv-media-box video {
            display: block;
            object-fit: cover;
            background: #000;
        }
        .graduate-video-card .gv-media-box .embed-responsive::before {
            display: none;
        }
        .graduate-video-card h3 {
            font-size: 1rem;
            padding: 14px 16px;
            margin: 0;
            line-height: 1.4;
        }
    </style>
@endpush

@section('content')
    <section class="site-top-banner search-top-banner opacity-04 position-relative">
        <img src="{{ $bannerImage }}" class="img-cover" alt=""/>

        <div class="container h-100">
            <div class="row h-100 align-items-center justify-content-center text-center">
                <div class="col-12 col-md-9 col-lg-7">
                    <div class="top-search-categories-form">
                        <h1 class="text-white font-30 mb-15">{{ $title }}</h1>
                        @if(!empty($bannerDescription))
                            <p class="text-white font-16 mb-10">{{ $bannerDescription }}</p>
                        @endif
                        <span class="course-count-badge py-5 px-10 text-white rounded">{{ $videos->count() }} فيديو</span>

                        <div class="mt-15">
                            <a href="/graduates" class="text-white font-16" style="text-decoration: underline;">
                                الخريجون
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="container mt-30 mb-50">
        <div class="row">
            @forelse($videos as $video)
                @php $embed = $video->resolvedEmbedUrl(); @endphp
                <div class="col-12 col-md-6 col-lg-4">
                    <div class="graduate-video-card">
                        @if($video->stored_video_path)
                            <div class="gv-media-box">
                                <video controls playsinline preload="metadata" title="{{ $video->title }}">
                                    <source src="{{ asset($video->stored_video_path) }}">
                                </video>
                            </div>
                        @elseif($embed)
                            <a href="{{ route('graduateVideos.show', ['graduate_video' => $video]) }}" class="d-block text-reset text-decoration-none">
                                <div class="gv-media-box">
                                    <div class="embed-responsive embed-responsive-16by9 video-facade" style="cursor:pointer;">
                                    @if($video->thumbnailUrl())
                                        <img src="{{ $video->thumbnailUrl() }}" alt="{{ $video->title }}" class="video-facade__thumb" loading="lazy" decoding="async">
                                    @else
                                        @php
                                            $listYtId = '';
                                            if (preg_match('#(?:youtube\.com/embed/|youtu\.be/)([\w-]{11})#', $embed, $listYtMatch)) {
                                                $listYtId = $listYtMatch[1];
                                            }
                                        @endphp
                                        @if($listYtId !== '')
                                            <img src="https://i.ytimg.com/vi/{{ $listYtId }}/hqdefault.jpg" alt="{{ $video->title }}" class="video-facade__thumb" loading="lazy" decoding="async">
                                        @else
                                            <div class="video-facade__thumb" style="background:linear-gradient(145deg,#1f3b64,#0d1525);min-height:100%;"></div>
                                        @endif
                                    @endif
                                    <span class="video-facade__play" aria-hidden="true"><span class="fa fa-play"></span></span>
                                </div>
                                </div>
                            </a>
                        @else
                            <div class="p-20 text-center text-muted">تعذر عرض الفيديو — راجع الرابط أو الملف في لوحة التحكم.</div>
                        @endif
                        <h3 class="font-16">{{ $video->title }}</h3>
                    </div>
                </div>
            @empty
                <div class="col-12 text-center py-50">
                    <p class="font-16 text-gray">لا توجد فيديوهات مضافة حالياً.</p>
                </div>
            @endforelse
        </div>
    </div>
@endsection
