<?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
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsOutline;
use Hyva\Theme\ViewModel\ProductAttributes;
use Hyva\Theme\ViewModel\ProductPage;
use Hyva\Theme\ViewModel\StoreConfig;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magento\Review\Block\Product\ReviewRenderer;

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

/** @var ProductPage $productViewModel */
$productViewModel = $viewModels->require(ProductPage::class);
/** @var ProductAttributes $attributesViewModel */
$attributesViewModel = $viewModels->require(ProductAttributes::class);
/** @var StoreConfig $storeConfig */
$storeConfig = $viewModels->require(StoreConfig::class);
/** @var HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);
/** @var Product $product */
$request = $block->getRequest();

$product = $productViewModel->getProduct();
$productType = $product->getTypeId();
$revealPrice = $request->getParam('reveal_price');
$storePhone = $storeConfig->getStoreConfig('general/store_information/phone');

/** @var ReviewRenderer $reviewRenderer */
$reviewRenderer = $block->getLayout()->createBlock(ReviewRenderer::class);
$reviewRenderer->setProduct($product);
$ratingHtml   = $reviewRenderer->getReviewsSummaryHtml($product, ReviewRenderer::FULL_VIEW, true);
$ratingCount = $reviewRenderer->getReviewsCount();
$ratingCount = !empty($ratingCount) ? $ratingCount : 0;
?>
<div class="w-ful">
    <div class="flex flex-col sm:flex-row justify-between my-4 font-sen">
        <?= $block->getChildHtml("product.info.stockstatus") ?>
        <?= $block->getChildHtml("alert.urls") ?>
    </div>
    <div class="my-2 flex justify-between">

        <?php if ($tierPriceBlock = $block->getChildHtml("product.price.tier")): ?>
            <div class="py-4 my-2 tier-price-container">
                <?= /** @noEscape */ $tierPriceBlock ?>
            </div>
        <?php endif; ?>

            <div role="group" aria-label="<?= $escaper->escapeHtmlAttr('Price') ?>">
                <?= $block->getChildHtml("product.info.price") ?>
            </div>
            <?php if ($productType === 'bundle'): ?>
                <span class="sr-only">
                    <?= $escaper->escapeHtml(__('The price depends on the chosen options')) ?>
                </span>
            <?php endif; ?>

        <div class="flex justify-end items-end space-x-1 text-sm text-gray-700">
            <?= $ratingHtml ?>
            <span>(<?= $escaper->escapeHtml($ratingCount ?: 0) ?>)</span>
        </div>

    </div>

    <dl id="product-details">
        <?php foreach ($block->getAttributes() as $attributeConfig):
            $attribute = $attributesViewModel->getAttributeFromLayoutConfig($attributeConfig); ?>
            <?php if ($value = $attribute['value'] ?? null): ?>
            <div class="flex border-t border-gray-300 py-2 last:mb-6 last:border-b attribute-<?=/* @noEscape */ $attribute['code'] ?: "" ?>">
                <dt class="w-1/2 text-left text-gray-700 product-detail-label font-sen">
                    <?= $escaper->escapeHtml($attribute['label']) ?>
                </dt>
                <dd class="w-1/2 ml-2 text-left text-gray-900 product-detail-value font-sen">
                    <?= $escaper->escapeHtml($value) ?>
                </dd>
            </div>
        <?php endif ?>
        <?php endforeach; ?>
    </dl>

    <?= $block->getChildHtml('product.info.form') ?>

    <div class="">

        <?php  if ($productType === 'bundle'): ?>
            <span class="sr-only">
                <?php  $escaper->escapeHtml(__('The price depends on the chosen options')) ?>
            </span>
        <?php endif; ?>

<!---->
<!--        <div role="group" aria-label="--><?php //= $escaper->escapeHtmlAttr('Price')?><!--">-->
<!--            --><?php //= $block->getChildHtml("product.info.price")?>
<!--        </div>-->
        <div class="flex items-center gap-1 md:gap-4 mt-4 ml-auto sm:mt-0">
            <?php if ($product->isSaleable()): ?>
                <?= $block->getChildHtml("product.info.quantity") ?>
                <?= $block->getChildHtml("product.info.addtocart") ?>
                <?= $block->getChildHtml("product.info.addtowishlistpro") ?>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($product->isSaleable() && $block->getChildHtml('addtocart.shortcut.buttons')): ?>
        <div class="flex mt-4 justify-end">
            <?= $block->getChildHtml('addtocart.shortcut.buttons') ?>
        </div>
    <?php endif; ?>

    <?php if ($additionalActions = $block->getChildHtml('product.info.additional.actions')): ?>
        <div class="flex mt-4 justify-end">
            <?= /** @noEscape */ $additionalActions; ?>
        </div>
    <?php endif; ?>

    <?php if (($tierPriceBlocks = $block->getChildHtml("product.info.combo"))): ?>
        <div class="py-4 ">
            <?= /** @noEscape */ $tierPriceBlocks ?>
        </div>
    <?php endif; ?>


    <?php if (($tierPriceBlock = $block->getChildHtml("product.price.tier"))): ?>
        <div class="py-4 my-2 tier-price-container">
            <?php /*= /** @noEscape */ /*$tierPriceBlock*/ ?>
        </div>
    <?php endif; ?>

    <?= $block->getChildHtml("product.info.additional") ?>

    <?= $block->getChildHtml("product.info.logos") ?>
</div>
