@php
    $resolveTestimonialField = function ($testimonial, $field) {
        $locale = mb_strtolower(app()->getLocale());
        $value = trim((string) getTranslateAttributeValue($testimonial, $field, $locale));

        if ($value !== '') {
            return $value;
        }

        if ($testimonial->relationLoaded('translations') && $testimonial->translations->count()) {
            foreach ($testimonial->translations as $translation) {
                $v = trim((string) ($translation->{$field} ?? ''));
                if ($v !== '') {
                    return $v;
                }
            }
        }

        $defaultLocale = mb_strtolower(getDefaultLocale());

        return trim((string) getTranslateAttributeValue($testimonial, $field, $defaultLocale));
    };

    $mergedReviews = collect();

    if ($course->reviews && $course->reviews->count() > 0) {
        foreach ($course->reviews as $review) {
            if (empty($review->creator)) {
                continue;
            }

            $text = clean($review->description, 'description');
            if (empty(trim(strip_tags($text)))) {
                continue;
            }

            $mergedReviews->push([
                'name' => $review->creator->full_name ?? '',
                'text' => $text,
                'avatar' => $review->creator->getAvatar(48),
                'rate' => (int) ($review->rates ?? 5),
                'source' => 'student',
            ]);
        }
    }

    $testimonials = $course->relationLoaded('instructor_testimonials')
        ? $course->instructor_testimonials
        : $course->instructor_testimonials()->with('translations')->get();

    foreach ($testimonials as $testimonial) {
        $name = $resolveTestimonialField($testimonial, 'name');
        $comment = $resolveTestimonialField($testimonial, 'comment');

        if ($name === '' || $comment === '') {
            continue;
        }

        $mergedReviews->push([
            'name' => $name,
            'text' => clean($comment, 'comment'),
            'avatar' => null,
            'rate' => 5,
            'source' => 'admin',
        ]);
    }

    $hasAny = $mergedReviews->count() > 0;
@endphp

@if($hasAny)
<section class="cp-section cp-reviews cp-reviews--under-curriculum cp-animate" id="cp-testimonials">
        @include('web.default.includes.premium.section_header', [
            'title' => trans('webinars.landing_testimonials_title'),
            'align' => 'center',
            'class' => 'mb-30 cp-section-title-wrap',
        ])

        <div class="cp-reviews__slider-wrap">
            <div class="swiper cp-reviews-swiper">
                <div class="swiper-wrapper">
                    @foreach($mergedReviews as $slide)
                        <div class="swiper-slide">
                            <article class="cp-review-card">
                                <div class="cp-review-card__top">
                                    @if(!empty($slide['avatar']))
                                        <img src="{{ $slide['avatar'] }}" alt="" class="cp-review-card__avatar" width="48" height="48" loading="lazy">
                                    @else
                                        <span class="cp-review-card__initials">{{ mb_substr($slide['name'], 0, 1) }}</span>
                                    @endif
                                    <div class="cp-review-card__meta">
                                        <p class="cp-review-card__name">{{ $slide['name'] }}</p>
                                        <div class="cp-review-card__stars" aria-hidden="true">
                                            @for($i = 1; $i <= 5; $i++)
                                                <span class="cp-star {{ $i <= $slide['rate'] ? 'is-on' : '' }}">★</span>
                                            @endfor
                                        </div>
                                    </div>
                                </div>
                                <div class="cp-review-card__text">{!! $slide['text'] !!}</div>
                            </article>
                        </div>
                    @endforeach
                </div>
            </div>
            @if($mergedReviews->count() > 1)
                <button type="button" class="cp-reviews-swiper-nav cp-reviews-swiper-prev" aria-label="{{ trans('webinars.landing_reviews_prev') }}">
                    <i data-feather="chevron-right" width="22" height="22" aria-hidden="true"></i>
                </button>
                <button type="button" class="cp-reviews-swiper-nav cp-reviews-swiper-next" aria-label="{{ trans('webinars.landing_reviews_next') }}">
                    <i data-feather="chevron-left" width="22" height="22" aria-hidden="true"></i>
                </button>
            @endif
            <div class="swiper-pagination cp-reviews-swiper-pagination"></div>
        </div>
</section>
@endif
