<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\ProductAttributes;
use Hyva\Theme\ViewModel\ProductCompare;
use Hyva\Theme\ViewModel\ProductPage;
use Hyva\Theme\ViewModel\Wishlist;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @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 Product $product */
$product = $productViewModel->getProduct();
$productType = $product->getTypeId();

?>
<div class="card mb-6">
    <?= $block->getChildHtml('product.info.title') ?>

    <div class="my-2 flex">
        <?= $block->getChildHtml('product.info.review') ?>
    </div>

    <?php if ($shortDescription = $productViewModel->getShortDescription()): ?>
        <div class="my-4 leading-relaxed product-description prose"><?= /* @noEscape */ $shortDescription ?></div>
    <?php endif; ?>

    <div class="flex flex-col sm:flex-row justify-between my-4">
        <div class="inline-flex flex-wrap items-center gap-x-6">
            <?= $block->getChildHtml("product.info.stockstatus") ?>
            <?= $block->getChildHtml("product.info.stockqty") ?>
        </div>
        <?= $block->getChildHtml("alert.urls") ?>
    </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">
                        <?= $escaper->escapeHtml($attribute['label']) ?>
                    </dt>
                    <dd class="w-1/2 ml-2 text-left text-gray-900 product-detail-value">
                        <?= $escaper->escapeHtml($value) ?>
                    </dd>
                </div>
            <?php endif ?>
        <?php endforeach; ?>
    </dl>

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

    <div class="flex flex-col sm:flex-row items-end my-4">

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

        <div role="group" aria-label="<?= $escaper->escapeHtmlAttr('Price') ?>">
            <?= $block->getChildHtml("product.info.price") ?>
        </div>

        <div class="flex gap-2 mt-4 ml-auto sm:mt-0">
            <?php if ($product->isSaleable()): ?>
                <?= $block->getChildHtml("product.info.quantity") ?>
                <?= $block->getChildHtml("product.info.addtocart") ?>
            <?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 ($tierPriceBlock = $block->getChildHtml("product.price.tier")): ?>
        <div class="py-4 my-2 tier-price-container">
            <?= /** @noEscape */ $tierPriceBlock ?>
        </div>
    <?php endif; ?>

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