<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\LucideIcons;
use Magento\Framework\Escaper;
use Magento\Review\Block\Product\ReviewRenderer;

/** @var ReviewRenderer $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var LucideIcons $lucideIcons */
$lucideIcons = $viewModels->require(LucideIcons::class);

$rating = $block->getRatingSummary();
$ratingSteps = 5;
$starsFilled = is_numeric($rating) ? floor($rating / 100 * $ratingSteps) : 0;
$starFragment = $rating / 100 * $ratingSteps - $starsFilled;
$starsEmpty = floor($ratingSteps - $starsFilled - $starFragment);
$starIconSize = $block->getData('rating_icon_size') ?: 20;

$reviewCount = $block->getReviewsCount();
$product = $block->getProduct();
?>
<?php if ($block->isReviewEnabled()): ?>
    <a
        <?php if ($reviewCount): ?>
            href="#customer-review-list"
            itemprop="aggregateRating"
            itemscope
            itemtype="https://schema.org/AggregateRating"
        <?php else: ?>
            href="#review-form"
        <?php endif; ?>
    >
        <?php if ($reviewCount): ?>
            <span hidden itemprop="ratingValue"><?= $starsFilled + $starFragment; ?></span>
            <span hidden itemprop="reviewCount"><?= (int) $reviewCount; ?></span>
        <?php endif; ?>
        <span
            role="img"
            class="rating-summary"
            aria-label="<?= $escaper->escapeHtmlAttr($rating
                ? __('%1 rating. %2 out of %3 stars', $product->getName(), $starsFilled + $starFragment, $ratingSteps)
                : __('Be the first to review this product')) ?>"
        >
            <?php if ($rating): ?>
                <?php $i = 0; ?>
                <?php while ($i < $starsFilled): ?>
                    <?= $lucideIcons->starHtml('fill-current', $starIconSize, $starIconSize, ['aria-hidden' => 'true']); ?>
                    <?php $i++; ?>
                <?php endwhile; ?>
                <?php if ($starFragment): ?>
                    <span>
                        <svg
                            xmlns="http://www.w3.org/2000/svg"
                            viewBox="0 0 24 24"
                            width="0"
                            height="0"
                            aria-hidden="true"
                        >
                            <defs>
                                <linearGradient id="partialFill<?= /* @noEscape */ $block->getProduct()->getId() ?>">
                                    <stop offset="0%" stop-color="currentColor"/>
                                    <stop offset="<?= $starFragment * 100 ?>%" stop-color="currentColor"/>
                                    <stop offset="<?= $starFragment * 100 ?>%" stop-color="#0000"/>
                                    <stop offset="100%" stop-color="#0000"/>
                                </linearGradient>
                            </defs>
                        </svg>
                        <?= $lucideIcons->starHtml('', $starIconSize, $starIconSize, [
                            'style' => 'fill: url(#partialFill' . (int)$block->getProduct()->getId() . ')',
                            'aria-hidden' => 'true'
                        ]); ?>
                    </span>
                <?php endif; ?>
                <?php $i = 0; ?>
                <?php while ($i < $starsEmpty): ?>
                    <?= $lucideIcons->starHtml('', $starIconSize, $starIconSize, ['aria-hidden' => 'true']); ?>
                    <?php $i++; ?>
                <?php endwhile; ?>
            <?php else: ?>
                <?php $i = 0; ?>
                <?php while ($i < $ratingSteps): ?>
                    <?= $lucideIcons->starHtml('', $starIconSize, $starIconSize, ['aria-hidden' => 'true']); ?>
                    <?php $i++; ?>
                <?php endwhile; ?>
            <?php endif; ?>
        </span>
    </a>
<?php endif; ?>
