<?php
/**
 * Shared home product card partial.
 * Expects: $product, $__badgePresets, $__badgeLegacyMap, $__cardAutoRotate
 */

$price      = $product['price'] ?? null;
$oldPrice   = $product['old_price'] ?? null;
$rating     = (int)($product['rating'] ?? 5);
$discount   = ($oldPrice && $price && $oldPrice > $price)
    ? round((($oldPrice - $price) / $oldPrice) * 100)
    : null;

$categoryName = $product['category_name'] ?? '';
$__badgeText = trim((string)($product['badge_text'] ?? ''));
$__badgeKey = isset($__badgePresets[$__badgeText]) ? $__badgeText : ($__badgeLegacyMap[$__badgeText] ?? '');
$__badgeMeta = $__badgeKey !== '' ? ($__badgePresets[$__badgeKey] ?? null) : null;
$__badgeLabel = $__badgeMeta ? __($__badgeKey, $__badgeMeta['fallback']) : '';
$__cardImages = array_values(array_filter(array_unique(array_map('strval', (array)($product['images'] ?? $product['gallery'] ?? [])))));
$__featured = (string)($product['featured_image'] ?? '/themes/matjari/assets/img/placeholder.png');
if ($__featured !== '' && !in_array($__featured, $__cardImages, true)) {
    array_unshift($__cardImages, $__featured);
}
$__cardImagesJson = htmlspecialchars(json_encode($__cardImages, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT), ENT_QUOTES, 'UTF-8');
?>
<div class="product-card-stack">

    <?php if ($__badgeMeta): ?>
        <span class="product-badge product-card-floating-badge <?= htmlspecialchars($__badgeMeta['class'], ENT_QUOTES, 'UTF-8') ?>">
            <span class="badge-emoji"><?= htmlspecialchars($__badgeMeta['emoji'], ENT_QUOTES, 'UTF-8') ?></span>
            <span><?= htmlspecialchars($__badgeLabel, ENT_QUOTES, 'UTF-8') ?></span>
        </span>
    <?php elseif ($discount): ?>
        <span class="product-badge product-card-floating-badge badge-discount">-<?= $discount ?>%</span>
    <?php endif; ?>

    <div class="product-card-vertical">
        <a href="/product/<?= rawurlencode($product['slug'] ?? (string)(int)$product['id']) ?>"
           class="product-thumb"
           data-card-gallery='<?= $__cardImagesJson ?>'
           data-card-autorotate="<?= (int)$__cardAutoRotate ?>"
           data-card-active-layer="primary">

            <img src="<?= htmlspecialchars($__featured, ENT_QUOTES, 'UTF-8') ?>"
                 alt="<?= htmlspecialchars($product['title'], ENT_QUOTES, 'UTF-8') ?>"
                 class="product-thumb-image product-thumb-image--primary is-visible"
                 loading="lazy">

            <img src="<?= htmlspecialchars($__featured, ENT_QUOTES, 'UTF-8') ?>"
                 alt="<?= htmlspecialchars($product['title'], ENT_QUOTES, 'UTF-8') ?>"
                 class="product-thumb-image product-thumb-image--secondary"
                 loading="lazy"
                 aria-hidden="true">
        </a>

        <div class="product-body">

            <div class="product-body-top">

                <?php if ($categoryName): ?>
                    <p class="product-category">
                        <?= htmlspecialchars($categoryName, ENT_QUOTES, 'UTF-8') ?>
                    </p>
                <?php endif; ?>

                <h3 class="product-name">
                    <?= htmlspecialchars($product['title'], ENT_QUOTES, 'UTF-8') ?>
                </h3>

                <div class="product-rating">
                    <?php for ($i = 1; $i <= 5; $i++): ?>
                        <span class="<?= $i <= $rating ? 'star-filled' : 'star-empty' ?>">★</span>
                    <?php endfor; ?>
                </div>

            </div>

            <div class="product-body-bottom">

                <div class="product-prices">
                    <?php if ($oldPrice): ?>
                        <span class="price-old">
                            <?= htmlspecialchars($oldPrice, ENT_QUOTES, 'UTF-8') ?>
                        </span>
                    <?php endif; ?>

                    <?php if ($price): ?>
                        <span class="price-current">
                            <?= htmlspecialchars($price, ENT_QUOTES, 'UTF-8') ?>
                        </span>
                    <?php endif; ?>
                </div>

                <a href="/product/<?= rawurlencode($product['slug'] ?? (string)(int)$product['id']) ?>"
                   class="product-btn">
                    <?= __('actions.buy_now', 'Buy now') ?>
                </a>

            </div>

        </div>
    </div>
</div>
