<?php
$s = settings();
$cur = $s['currency'] ?? '₺';
$showNutrition = ($s['show_nutrition'] ?? '1') === '1';
$allergens = array_filter(array_map('trim', explode(',', (string)$product['allergens'])));
$hasNutrition = $product['calories'] || $product['protein'] || $product['carbs'] || $product['fat'] || $product['sugar'] || $product['caffeine'];
?>
<div class="product-detail">
    <div class="pd-media">
        <?php if (!empty($product['video'])): ?>
            <video autoplay muted loop playsinline webkit-playsinline preload="<?= video_preload(true) ?>" poster="<?= e(media_poster($product['image'])) ?>" disablePictureInPicture>
                <source src="<?= e(media_url($product['video'])) ?>" type="video/mp4">
            </video>
        <?php elseif (!empty($product['image'])): ?>
            <img src="<?= e(media_poster($product['image'])) ?>" alt="<?= e($product['name']) ?>" decoding="async" fetchpriority="high">
        <?php else: ?>
            <div class="pd-noimg">🍽️</div>
        <?php endif; ?>
        <a class="pd-back" href="<?= e($product['category_slug'] ? url('kategori/'.$product['category_slug']) : url('menu')) ?>">←</a>
    </div>

    <div class="pd-body container">
        <?php if (!empty($product['category_name'])): ?>
            <a class="pd-cat" href="<?= e(url('kategori/'.$product['category_slug'])) ?>"><?= e($product['category_name']) ?></a>
        <?php endif; ?>
        <div class="pd-head reveal">
            <h1><?= e($product['name']) ?></h1>
            <span class="pd-price"><?= e(money($product['price'], $cur)) ?></span>
        </div>

        <?php if (!empty($product['description'])): ?>
            <p class="pd-desc reveal"><?= e($product['description']) ?></p>
        <?php endif; ?>

        <?php if ($allergens): ?>
        <div class="allergens-block reveal">
            <div class="nutri-head">
                <h2>Alerjen Bilgisi</h2>
                <span class="nutri-tag">Yönetmeliğe uygun</span>
            </div>
            <p class="allergen-legal">Bu ürün aşağıdaki alerjenleri içerebilir (Türk Gıda Kodeksi — Gıda Etiketleme ve Tüketicileri Bilgilendirme Yönetmeliği):</p>
            <div class="allergen-tags">
                <?php foreach ($allergens as $a): ?>
                    <span class="allergen"><?= e($a) ?></span>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <?php if ($showNutrition && $hasNutrition): ?>
        <div class="nutrition reveal">
            <div class="nutri-head">
                <h2>Besin Değerleri</h2>
                <span class="nutri-tag">Porsiyon bilgisi</span>
            </div>

            <?php if ($product['calories']): ?>
            <div class="calorie-big">
                <span class="cal-num"><?= (int)$product['calories'] ?></span>
                <span class="cal-unit">kcal</span>
                <span class="cal-note">porsiyon başına (yaklaşık)</span>
            </div>
            <?php endif; ?>

            <div class="macros">
                <?php
                $macros = [
                    ['Protein', $product['protein'], 'g'],
                    ['Karbonhidrat', $product['carbs'], 'g'],
                    ['Yağ', $product['fat'], 'g'],
                    ['Şeker', $product['sugar'], 'g'],
                    ['Kafein', $product['caffeine'], 'mg'],
                ];
                foreach ($macros as [$label,$val,$unit]):
                    if ($val === null || $val === '' || (float)$val == 0) continue; ?>
                    <div class="macro">
                        <span class="macro-val"><?= e(rtrim(rtrim(number_format((float)$val,1,',','.'),'0'),',')) ?><small><?= $unit ?></small></span>
                        <span class="macro-label"><?= $label ?></span>
                    </div>
                <?php endforeach; ?>
            </div>

            <?php if (!empty($product['notes'])): ?>
                <p class="nutri-notes"><?= e($product['notes']) ?></p>
            <?php endif; ?>
            <p class="nutri-disclaimer">* Besin değerleri yaklaşıktır; hazırlanış ve porsiyona göre değişebilir.</p>
        </div>
        <?php endif; ?>

        <?php if (!empty($related)): ?>
        <div class="related reveal">
            <h2>Bunları da Beğenebilirsiniz</h2>
            <div class="feat-scroller">
                <?php foreach ($related as $r): ?>
                    <a class="feat-card" href="<?= e(url('urun/'.$r['slug'])) ?>">
                        <div class="feat-media">
                            <?php if (!empty($r['image'])): ?>
                                <img loading="lazy" decoding="async" src="<?= e(media_url($r['image'])) ?>" alt="<?= e($r['name']) ?>">
                            <?php else: ?><span class="feat-emoji">✨</span><?php endif; ?>
                        </div>
                        <div class="feat-info">
                            <h3><?= e($r['name']) ?></h3>
                            <span class="price"><?= e(money($r['price'], $cur)) ?></span>
                        </div>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
