@php
    $freeChapters = $course->chapters()->whereHas('chapterItems', function ($q) {
        $q->whereHas('file', function ($q2) {
            $q2->where('accessibility', 'free');
        });
    })->withCount('chapterItems')->get();

    $freeLessons = collect();
    if ($freeChapters->sum('chapter_items_count') > 0) {
        $freeLessons = App\Models\WebinarChapterItem::whereIn('chapter_id', $course->chapters()->pluck('id')->toArray())
            ->whereHas('file', function ($q2) {
                $q2->where('accessibility', 'free');
            })
            ->take(2)
            ->get();
    }
@endphp
@if($freeLessons->count() > 0)
<section class="cp-section cp-free-lessons cp-animate">
    <div class="container">
        @include('web.default.includes.premium.section_header', [
            'title' => trans('webinars.landing_free_lessons_title'),
            'align' => 'center',
            'class' => 'mb-30',
        ])
        <div class="row">
            @foreach($freeLessons as $lesson)
                @php
                    if (empty($lesson->file->file)) {
                        continue;
                    }
                    $ytarray = explode('/', $lesson->file->file);
                    $ytendstring = end($ytarray);
                    $ytendarray = explode('?v=', $ytendstring);
                    $ytendstring = end($ytendarray);
                    $ytendarray = explode('&', $ytendstring);
                    $ytcode = $ytendarray[0];
                @endphp
                <div class="col-md-6 mb-20 mb-md-0">
                    @include('web.default.includes.premium.video_facade', [
                        'embedUrl' => 'https://www.youtube.com/embed/'.$ytcode,
                        'youtubeId' => $ytcode,
                        'title' => $lesson->title ?? '',
                        'wrapperClass' => 'cp-free-lessons__video video-facade-wrap',
                    ])
                </div>
            @endforeach
        </div>
    </div>
</section>
@endif
