<?php
declare(strict_types=1);

use Hyva\Theme\ViewModel\CurrentProduct;
use Magento\Framework\Escaper;
use Magento\Review\Block\Product\ReviewRenderer;
use Banyo\CustomProduct\ViewModel\ReviewTab;

/** @var Escaper $escaper */
/** @var \Magento\Framework\View\Element\Template $block */
/** @var \Hyva\Theme\ViewModel\HyvaViewModelRegistry $viewModels */

// Get current product via Hyvä view model
$currentProduct = $viewModels->require(CurrentProduct::class);
$product = $block->getData('product') ?: $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}

// Get review block logic
/** @var \Banyo\CustomProduct\Block\Product\ReviewTab $reviewTabBlock */
$reviewTabBlock = $block->getLayout()->getBlock('review.section');

// ReviewRenderer
/** @var ReviewRenderer $reviewRenderer */
$reviewRenderer = $block->getLayout()->createBlock(ReviewRenderer::class);
$reviewRenderer->setProduct($product);

$ratingHtml = $reviewRenderer->getReviewsSummaryHtml($product, ReviewRenderer::FULL_VIEW, true);
$reviewCount = $reviewRenderer->getReviewsCount();
$ratingSummary = $reviewRenderer->getRatingSummary();

// Convert to 5-star scale and format
$ratingInStars = $ratingSummary / 100 * 5;
$formattedRating = number_format($ratingInStars, 1);
if ($formattedRating === '0.0') {

    $totalVotes = 0;
    $ratingData = [];

}else{
    $ratingData = $reviewTabBlock ? $reviewTabBlock->getAllStart() : [];
    $totalVotes = array_sum(array_column($ratingData, 'value'));
}


?>
<?php if ($ratingHtml): ?>
    <div class="flex flex-col md:flex-row gap-6 md:p-6 bg-white items-start md:items-center">

        <!-- Rating Summary & Breakdown -->
        <div class="flex flex-col sm:flex-row gap-6 items-start sm:items-center md:basis-4/5">

            <!-- Average Rating -->
            <div class="text-center sm:text-left">
                <div class="text-5xl font-bold leading-none"><?= $formattedRating ?></div>
                <div class="flex justify-center sm:justify-start mt-1 text-yellow-400 text-xl">
                    <?= $ratingHtml ?>
                </div>
                <div class="text-gray-500 text-sm mt-1">
                    <?= $escaper->escapeHtml((string)$reviewCount) ?> Review<?= $reviewCount !== 1 ? 's' : '' ?>
                </div>
            </div>

            <!-- Rating Breakdown -->
            <div class="flex flex-col gap-1">
                <?php for ($i = 5; $i >= 1; $i--): ?>
                    <?php
                    $count = (int)($ratingData[$i]['value'] ?? 0);
                    $width = $totalVotes ? round(($count / $totalVotes) * 100, 1) : 0;
                    ?>
                    <div class="flex items-center gap-2 text-sm">
                        <span class="w-12"><?= $i ?> Star<?= $i > 1 ? 's' : '' ?></span>
                        <div class="w-48 bg-gray-200 h-2 rounded-full overflow-hidden">
                            <div class="bg-yellow-400 h-2" style="width: <?= $width ?>%;"></div>
                        </div>
                        <span class="w-6 text-right"><?= $count ?></span>
                    </div>
                <?php endfor; ?>
            </div>

        </div>

        <!-- Write Review Button -->
        <div class="md:basis-1/5 md:text-right w-full md:w-auto">
            <a href="#review_form"
               class="btn bg-primary-darker text-white ">
                Write A Review
            </a>
        </div>

    </div>
<?php endif; ?>
