<?php
/**
 * Hyvä Themes - https://hyva.io
 */

declare(strict_types=1);

use Magento\Framework\Escaper;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template;
use Magento\Review\Block\Product\ReviewRenderer;
use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CurrentProduct;

/** @var Escaper $escaper */
/** @var AbstractBlock $sectionBlock */
/** @var Template $block */
/** @var ViewModelRegistry $viewModels */

/** @var CurrentProduct $currentProduct */
$currentProduct = $viewModels->require(CurrentProduct::class);

$product = $block->getData('product') ?: $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}

/** @var ReviewRenderer $reviewRenderer */
$reviewRenderer = $block->getLayout()->createBlock(ReviewRenderer::class);
$reviewRenderer->setProduct($product);

$ratingHtml   = $reviewRenderer->getReviewsSummaryHtml($product, ReviewRenderer::FULL_VIEW, true);
$ratingCount  = $reviewRenderer->getReviewsCount();
$sectionTitle = $sectionBlock->getTitle();
?>

<div class="flex justify-between items-center md:px-6 py-3 ">
    <!-- Left Side: Section Title -->
    <div class="flex items-center space-x-4">
        <h2 class="text-white text-2xl font-medium">
            <?= $sectionTitle ? $escaper->escapeHtml($sectionTitle) : '&nbsp;' ?>
        </h2>
    </div>

    <!-- Right Side: Rating -->
    <div class="flex items-center space-x-2">
        <?php if ($sectionTitle && str_contains((string) $sectionTitle, 'Reviews')): ?>
            <span class="flex items-center ml-4">
        <?= $ratingHtml ?>
                <?php if ($ratingCount > 0): ?>
                    <span class="ml-2 text-gray-900 text-2xl font-medium text-white">(<?= $escaper->escapeHtml($ratingCount) ?>)</span>
                <?php endif; ?>
    </span>
        <?php else: ?>
            &nbsp;
        <?php endif; ?>

    </div>
</div>
