<?php

/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsOutline;
use Hyva\Theme\ViewModel\HeroiconsSolid;
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 HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);

/** @var HeroiconsSolid $heroiconsSolid */
$heroiconsSolid = $viewModels->require(HeroiconsSolid::class);

$reviewCollection = $block->getReviewsCollection();
/*$limit = (int) ($block->getRequest()->getParam('limit', 3));
$page = (int) ($block->getRequest()->getParam('review_page', 1));
$allowed = [3, 6, 12];*/

/*if (!in_array($limit, $allowed)) {
    $limit = 3;
}*/

/*$reviewCollection->setPageSize($limit);
$reviewCollection->setCurPage($page);*/
$reviewCollection->load()->addRateVotes();
$limit = 10;
if ($toolbar = $block->getChildBlock('review_list.toolbar')) {
    $toolbar->setCollection($reviewCollection);
    /*$toolbar->setData('limit', $limit);*/
    $toolbar->setPageVarName('review_page'); // 👈 force it to use review_page instead of p
}


$items = $reviewCollection->getItems();
$format = $block->getDateFormat() ?: \IntlDateFormatter::SHORT;
$formatLong = $block->getDateFormat() ?: \IntlDateFormatter::LONG;
$headingTag = $block->getData('heading_tag') ?: 'h3';
$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"
    >
      <!--  <div class="flex px-6 pt-6 pb-3 mb-6 md:flex-row border-b-2 border-gray-300">
            <?php /*if (!$block->getHideTitle()): */?>
                <<?php /*= /* @noEscape */ $headingTag ?> class="atext-gray-900 text-2xl title-font font-base text-center md:text-left w-full">
                    <?php /*= $escaper->escapeHtml(__('Customer Reviews')) */?>
                </<?php /*= /* @noEscape */ $headingTag ?>>
            <?php /*endif */?>
        </div>-->
        <div class=" w-full">
            <?php foreach ($reviewCollection->getItems() as $review): ?>
                <div itemscope itemprop="review" itemtype="https://schema.org/Review"
                    class="border p-6 mb-5 rounded-md pb-4  last:mb-0">
                    <?php if (count($review->getRatingVotes())): ?>
                        <?php
                        $totalPercent = 0;
                        $votesCount = 0;

                        foreach ($review->getRatingVotes() as $vote) {
                            $totalPercent += $vote->getPercent(); // 0–100 scale
                            $votesCount++;
                        }

                        $averagePercent = $votesCount ? ($totalPercent / $votesCount) : 0;

                        // Convert percent to star count (0–5)
                        $ratingSteps = 5;
                        $starsFilled = floor($averagePercent / 100 * $ratingSteps);
                        $partial = ($averagePercent / 100 * $ratingSteps) - $starsFilled;
                        $starsEmpty = $ratingSteps - $starsFilled - ($partial > 0 ? 1 : 0);
                        ?>

                        <div class="flex items-end  flex-wrap">
                            <?php for ($i = 0; $i < $starsFilled; $i++): ?>
                                <?= $heroiconsSolid->starHtml('text-yellow-400', 24, 24, ['aria-hidden' => 'true']); ?>
                            <?php endfor; ?>

                            <?php if ($partial > 0): ?>
                                <span class="relative w-6 h-6 inline-block">
            <span class="absolute inset-0 w-1/2 overflow-hidden">
                <?= $heroiconsSolid->starHtml('text-yellow-400', 24, 24, ['aria-hidden' => 'true']); ?>
            </span>
            <?= $heroiconsSolid->starHtml('text-gray-400', 24, 24, ['aria-hidden' => 'true']); ?>
        </span>
                            <?php endif; ?>

                            <?php for ($i = 0; $i < $starsEmpty; $i++): ?>
                                <?= $heroiconsSolid->starHtml('text-gray-400', 24, 24, ['aria-hidden' => 'true']); ?>
                            <?php endfor; ?>


                            <!--  Rating Text -->
                            <div class="ml-auto text-sm text-gray-700">
                                <?= $escaper->escapeHtml(
                                    $block->formatDate(
                                        $review->getCreatedAt(),
                                        $formatLong
                                    )
                                ) ?>
                            </div>
                        </div>

                    <?php endif; ?>

                    <div class="text-lg font-bold my-2" itemprop="name">
                        <?= $escaper->escapeHtml($review->getTitle()) ?>
                    </div>

                    <div class="mt-2" itemprop="description">
                        <?= /* @noEscape */ nl2br($escaper->escapeHtml($review->getDetail())) ?>
                    </div>
                    <br/>
                    <strong class="mt-3" itemprop="author" itemscope itemtype="https://schema.org/Person">
                        <span itemprop="name"><?= $escaper->escapeHtml($review->getNickname()) ?></span>
                    </strong>

                </div>
            <?php endforeach; ?>
        </div>
        <?php if ($toolbar): ?>
        <div class="toolbar review-toolbar mt-4">
            <?= $toolbar->toHtml() ?>
        </div>
        <?php endif; ?>
    </div>
<?php endif; ?>
