<?php
use Magento\Framework\View\Element\Template;
use Magento\Framework\Escaper;
use Banyo\CustomHomePage\ViewModel\BanyoOffers;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var BanyoOffers $viewModel */
$viewModel = $block->getData('viewModel');

// Get category name from block configuration or use default
$categoryName = $block->getData('category_name') ?? 'Banyo Offers';
$pageSize = (int)($block->getData('page_size') ?? 8);
$imageConfigPath = $block->getData('image_config_path');

// Get products data for the specified category
$data = $viewModel->getCategoryProductsData($categoryName, $pageSize, $imageConfigPath);

if (empty($data['products'])) return;

$products = $data['products'];
$categoryUrl = $data['url'];
$categoryName = $data['name'] ?: $categoryName;
$imageUrl = $data['image'];

// Get price and review renderers
$priceRenderer = $block->getLayout()->getBlock('product.price.render.default');
$reviewRenderer = $block->getLayout()->getBlock('product.review.renderer');
?>

<?php
// Get the banner image for Banyo Offers
$firstImage = $viewModel->getSlideImageUrl('banyo_theme/banyo_offers_homepage_image/banyo_offers_image');
?>
<section class="container mx-auto my-12 px-4 hidden" aria-label="<?= $escaper->escapeHtmlAttr($categoryName) ?>">
    <?php if ($firstImage): ?>
    <div class="mb-8 hidden">
        <img src="<?= $escaper->escapeUrl($firstImage) ?>" alt="<?= $escaper->escapeHtmlAttr($categoryName) ?>" class="w-full rounded-lg">
    </div>
    <?php endif; ?>
    <div class="flex justify-between items-center mb-8">
        <h2 class="text-2xl md:text-3xl font-bold"><?= $escaper->escapeHtml($categoryName) ?></h2>
        <?php if ($categoryUrl): ?>
            <a href="<?= $escaper->escapeUrl($categoryUrl) ?>" class="text-blue-600 hover:underline">
                <?= $escaper->escapeHtml(__('View All')) ?>
            </a>
        <?php endif; ?>
    </div>

    <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
        <?php foreach ($products as $product): ?>
            <div class="bg-white shadow-lg rounded-xl overflow-hidden transition-all duration-300 hover:shadow-xl">
                <a href="<?= $escaper->escapeUrl($product->getProductUrl()) ?>" class="block">
                    <img src="<?= $escaper->escapeUrl($viewModel->getProductImageUrl($product)) ?>"
                         alt="<?= $escaper->escapeHtmlAttr($product->getName()) ?>"
                         class="w-full h-56 object-contain">
                </a>

                <div class="p-4">
                    <h3 class="font-semibold text-lg mb-1 line-clamp-2">
                        <a href="<?= $escaper->escapeUrl($product->getProductUrl()) ?>"
                           class="hover:text-blue-600 transition-colors">
                            <?= $escaper->escapeHtml($product->getName()) ?>
                        </a>
                    </h3>

                    <!-- Rating -->
                    <?php
                    $ratingPercent = min(100, max(0, $viewModel->getProductRatingSummary($product->getId())));
                    $stars = round(($ratingPercent / 100) * 5, 1);
                    ?>
                    <div class="flex items-center mb-2">
                        <div class="flex text-yellow-400">
                            <?php for ($i = 1; $i <= 5; $i++): ?>
                                <svg class="w-5 h-5 <?= $i <= $stars ? 'fill-current' : 'fill-none' ?>"
                                     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>
                        <span class="text-sm text-gray-600 ml-1"><?= number_format($stars, 1) ?></span>
                    </div>

                    <!-- Pricing -->
                    <?php if ($priceRenderer): ?>
                        <div class="mb-2">
                            <?= $priceRenderer->render('final_price', $product, [
                                'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
                            ]) ?>
                        </div>
                    <?php endif; ?>

                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>
