<?php

use Banyo\CustomCategory\ViewModel\BestSeller;
use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\SvgIcons;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Escaper;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */
/** @var BestSeller $bestSellerViewModel */

$bestSellerViewModel = $viewModels->require(\Banyo\CustomCategory\ViewModel\BestSeller::class);
$data = $bestSellerViewModel->getBestSellerData();

if(isset($data['products']) && isset($data['url']) && isset($data['name'])){
    $bestSellerProducts = $data['products'];
    $bestSellerUrl = $data['url'];
    $bestSellerName = $data['name'];
}
if (!empty($bestSellerProducts)):

?>
<!-- Bestseller -->
<section class="best-seller">
    <section class="best-seller best-3">
        <div class="px-3 2xl:container 3xl:max-w-screen-2xl py-4 md:py-5">
            <h2 class="text-center text-2xl font-semibold mb-8 line-bord relative"><span><?= $bestSellerName; ?></span></h2>
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                <?php foreach ($bestSellerProducts as $product): ?>
                    <div class="bg-white rounded-lg overflow-hidden shadow-md text-center">
                        <?php
                        $imageUrl = $this->helper(\Magento\Catalog\Helper\Image::class)
                            ->init($product, 'product_base_image')
                            ->getUrl();
                        $finalPrice = $product->getFinalPrice();
                        $regularPrice = $product->getPrice();
                        ?>
                        <a href="<?= $product->getProductUrl() ?>" class="block">
                            <img src="<?= $imageUrl ?>" alt="<?= $product->getName(); ?>"
                                 class="w-full object-cover mb-3">
                            <h3 class="product-item-link text-xs md:text-sm lg:text-base font-medium text-black font-sen h-8 md:h-10 lg:min-h-12 line-clamp-2"><?= $product->getName(); ?></h3>
                        </a>
                        <div class="flex items-center mt-3 justify-center pb-0">
                            <p class="text-red-500 font-bold mr-2 text-base lg:text-2xl">£<?= number_format($finalPrice,2); ?></p>
                            <?php if ($regularPrice > $finalPrice): ?>
                                <p class="line-through text-gray-500 font-semibold">
                                    £<?= number_format($regularPrice, 2); ?>
                                </p>
                            <?php endif; ?>
                        </div>
                        <!-- Rating -->
                        <?php
                            $ratingSummary = $product->getRatingSummary();
                            $stars = $ratingSummary ? round(($ratingSummary / 100) * 5) : 0;
                        ?>
                      <div class="flex items-center justify-center mb-2">
                          <div class="flex items-center mt-2">
                              <?php for ($i = 1; $i <= 5; $i++): ?>
                                  <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 <?= $i <= $stars ? 'text-yellow-400' : 'text-gray-300' ?>"
                                       fill="currentColor" viewBox="0 0 24 24">
                                      <path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2
                                                      9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/>
                                  </svg>
                              <?php endfor; ?>
                          </div>
                      </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <div class="view-btn mt-6 block text-center">
                <a href="<?= $bestSellerUrl ?>" class="text-red-500 font-semibold">View All</a>
            </div>
        </div>
    </section>
</section>
<?php
else:
echo ' <div class="container p-6"><p>No Best Seller products available.</p> </div>';
endif;
?>
