<?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\View\Helper\SecureHtmlRenderer;
use Magento\Framework\Escaper;
use Magento\Review\Block\Product\View as ProductReview;
use Magento\Theme\Block\Html\Pager;

// phpcs:disable Generic.Files.LineLength.TooLong

/** @var ProductReview $block */
/** @var SecureHtmlRenderer $secureRenderer */
/** @var Escaper $escaper */
/** @var Pager $toolbar */
/** @var ViewModelRegistry $viewModels */

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

$starIconSize = $block->getData('rating_icon_size') ?: 18;

$reviewCollection = $block->getReviewsCollection();

// Note: Setting the collection on the toolbar block applies pagination, so it has to happen before collection loading.
if ($toolbar = $block->getChildBlock('review_list.toolbar')) {
    $toolbar->setCollection($reviewCollection);
}
$reviewCollection->load()->addRateVotes();

$items = $reviewCollection->getItems();
$format = $block->getDateFormat() ?: \IntlDateFormatter::SHORT;
$formatLong = $block->getDateFormat() ?: \IntlDateFormatter::LONG;
$productName = $block->getProduct()->getName();

?>
<?php if (count($items)): ?>
    <div
        class="mx-auto py-6 items-center"
        id="customer-review-list"
        aria-label="<?= $escaper->escapeHtmlAttr(__('Customer Reviews')) ?>"
        tabindex="-1"
        role="group"
    >
        <?php if (!$block->getHideTitle()): ?>
            <h2 class="mb-6 text-2xl text-center md:text-left">
                <?= $escaper->escapeHtml(__('Customer Reviews')) ?>
            </h2>
        <?php endif ?>
        <div class="card w-full px-6 py-3">
            <?php foreach ($items as $review): ?>
                <div itemscope itemprop="review" itemtype="https://schema.org/Review"
                    class="border-b pb-4 my-4 border-gray-300 last:border-0 last:mb-0">
                    <?php if (count($review->getRatingVotes())): ?>
                        <div class="table">
                        <?php foreach ($review->getRatingVotes() as $vote): ?>
                            <?php
                                $rating = $vote->getPercent();
                                $ratingSteps = 5;
                                $starsFilled = is_numeric($rating) ? floor($rating / 100 * $ratingSteps) : 0;
                                $starsEmpty = floor($ratingSteps - $starsFilled);
                            ?>
                            <div class="table-row" itemprop="reviewRating" itemscope itemtype="https://schema.org/Rating">
                                <div class="table-cell align-middle pe-4">
                                    <?= $escaper->escapeHtml($vote->getRatingCode()) ?>
                                </div>
                                <div class="table-cell align-middle">
                                    <span class="hidden" itemprop="ratingValue"><?= $escaper->escapeHtmlAttr($starsFilled); ?></span>
                                    <div
                                        class="rating-summary"
                                        role="img"
                                        aria-label="<?= $escaper->escapeHtmlAttr(__('%1 rating. %2 out of %3 stars', $productName, $starsFilled, $ratingSteps))?>"
                                    >
                                        <?php $i = 0; ?>
                                        <?php while ($i < $starsFilled): ?>
                                            <?= $lucideIcons->starHtml('fill-current', $starIconSize, $starIconSize, ['aria-hidden' => 'true']); ?>
                                            <?php $i++; ?>
                                        <?php endwhile; ?>
                                        <?php $i = 0; ?>
                                        <?php while ($i < $starsEmpty): ?>
                                            <?= $lucideIcons->starHtml('', $starIconSize, $starIconSize, ['aria-hidden' => 'true']); ?>
                                            <?php $i++; ?>
                                        <?php endwhile; ?>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    <div class="text-lg my-2" itemprop="name">
                        <?= $escaper->escapeHtml($review->getTitle()) ?>
                    </div>
                    <div>
                        <span><?= $escaper->escapeHtml(__('Review by')) ?></span>
                        <strong itemprop="author" itemscope itemtype="https://schema.org/Person">
                            <span itemprop="name"><?= $escaper->escapeHtml($review->getNickname()) ?></span>
                        </strong>
                            <time
                                class="text-gray-700 inline-block ml-2"
                                itemprop="datePublished"
                                datetime="<?= $escaper->escapeHtmlAttr($block->formatDate($review->getCreatedAt(), $format)); ?>"
                            >
                                <span class="sr-only">
                                    <?= $escaper->escapeHtml(
                                        $block->formatDate(
                                            $review->getCreatedAt(),
                                            $formatLong
                                        )
                                    ) ?>
                                </span>
                                <span aria-hidden="true">
                                    <?= $escaper->escapeHtml(
                                        $block->formatDate($review->getCreatedAt(), $format)
                                    ) ?>
                                </span>
                            </time>
                    </div>
                    <div class="mt-2" itemprop="description">
                        <?= /* @noEscape */ nl2br($escaper->escapeHtml($review->getDetail())) ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php if ($toolbar): ?>
        <div class="toolbar review-toolbar mt-4">
            <?= $toolbar->toHtml() ?>
        </div>
        <?php endif; ?>
    </div>
<?php endif; ?>
