<?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\ProductPage;
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 Product $product */
$product = $productViewModel->getProduct();

// Get product data
$productName = $product->getName();
$productSku = $product->getSku();
$productDescription = $product->getDescription() ? strip_tags($product->getDescription()) : $product->getShortDescription();
$productDescription = $productDescription ? strip_tags($productDescription) : '';

// Get product brand
$brandName = '';
$brandAttribute = $product->getResource()->getAttribute('brand');
if ($brandAttribute) {
    $brandName = $brandAttribute->getFrontend()->getValue($product);
}

// Get product image
$productImage = '';
if ($product->getMediaGalleryImages()->count() > 0) {
    $productImage = $product->getMediaGalleryImages()->getFirstItem()->getUrl();
}
if (!$productImage && $product->getImage()) {
    $productImage = $block->getUrl('pub/media/catalog/product') . $product->getImage();
}

// Get product URL
$productUrl = $product->getProductUrl();

// Get product price
$productPrice = $product->getFinalPrice();
$currencyCode = 'GBP';

// Get current date for priceValidUntil
$currentDate = new \DateTime();
$priceValidUntil = $currentDate->format('Y-m-d');

// Get product availability
$availability = $product->isAvailable() ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock';

// Get actual product rating values
// getRatingSummary() returns a value from 0-100, divide by 20 to get 0-5 scale
$ratingValue = $product->getRatingSummary() ? number_format($product->getRatingSummary() / 20, 1, '.', '') : '5.0';
// getReviewsCount() returns the number of reviews for the product
$reviewCount = $product->getReviewsCount() ? (string)$product->getReviewsCount() : '0';
?>
<?php
// Create schema data array
$schemaData = [
    "@context" => "https://schema.org",
    "@type" => "Product",
    "name" => $productName,
    "sku" => $productSku,
    "description" => $productDescription,
    "brand" => [
        "@type" => "Brand",
        "name" => $brandName ?: 'Banyo'
    ],
    "offers" => [
        "@type" => "Offer",
        "url" => $productUrl,
        "price" => number_format($productPrice, 2, '.', ''),
        "priceCurrency" => $currencyCode,
        "priceValidUntil" => $priceValidUntil,
        "sku" => $productSku,
        "availability" => $availability
    ]
];

// Add aggregateRating only if ratings exist
if ($product->getRatingSummary() && $product->getReviewsCount() > 0) {
    $schemaData["aggregateRating"] = [
        "@type" => "AggregateRating",
        "ratingValue" => $ratingValue,
        "reviewCount" => $reviewCount
    ];
}

// Add image if available
if ($productImage) {
    $schemaData["image"] = $productImage;
}
?>
<script type="application/ld+json">
<?= json_encode($schemaData) ?>
</script>
