<?php
use function App\Core\{e, url};

$category = $category ?? ['name' => 'Kategori', 'slug' => $slug ?? '', 'description' => '', 'banner' => '', 'icon' => '', 'mod_count' => 0];
$activeCategory = $category;
$children = $children ?? [];
$mods = $mods ?? [];
$sort = $sort ?? 'newest';
$sortOptions = [
    'newest' => 'En yeni',
    'downloaded' => 'En cok indirilen',
    'viewed' => 'En cok goruntulenen',
    'popular' => 'Populer',
    'premium' => 'Premium',
    'free' => 'Ucretsiz',
];
$heroStyle = !empty($activeCategory['banner']) ? ' style="--category-banner: url(' . e(url($activeCategory['banner'])) . ');"' : '';
?>
<section class="category-hero container<?= !empty($activeCategory['banner']) ? ' has-banner' : '' ?>"<?= $heroStyle ?>>
    <span class="category-card-backdrop" aria-hidden="true"></span>
    <div>
        <p class="eyebrow">Kategori</p>
        <h1><?= e($activeCategory['name']) ?></h1>
        <?php if (!empty($activeCategory['description'])): ?><p><?= e($activeCategory['description']) ?></p><?php endif; ?>
    </div>
    <strong><?= number_format((int) ($activeCategory['mod_count'] ?? count($mods)), 0, ',', '.') ?> mod</strong>
</section>

<section class="container section">
    <div class="chips filter-bar" aria-label="Siralama">
        <?php foreach ($sortOptions as $key => $label): ?>
            <a class="<?= $sort === $key ? 'is-active' : '' ?>" href="<?= url('/kategori/' . $activeCategory['slug'] . '?sirala=' . $key) ?>"><?= e($label) ?></a>
        <?php endforeach; ?>
    </div>
</section>

<?php if (!empty($categories)): ?>
    <section class="container section">
        <div class="section-head">
            <h2>Ana kategori bloklari</h2>
        </div>
        <div class="category-grid category-grid-main">
            <?php foreach ($categories as $categoryItem): ?>
                <?php $category = $categoryItem; require BASE_PATH . '/app/Views/partials/category-card.php'; ?>
            <?php endforeach; ?>
        </div>
    </section>
<?php endif; ?>

<?php if (!empty($children)): ?>
    <section class="container section">
        <div class="section-head">
            <h2>Alt kategoriler</h2>
        </div>
        <div class="category-grid">
            <?php foreach ($children as $categoryItem): ?>
                <?php $category = $categoryItem; require BASE_PATH . '/app/Views/partials/category-card.php'; ?>
            <?php endforeach; ?>
        </div>
    </section>
<?php endif; ?>

<section class="container section">
    <div class="section-head">
        <h2><?= !empty($children) ? 'Bu kategorideki modlar' : 'Modlar' ?></h2>
    </div>
    <?php if (!empty($mods)): ?>
        <div class="grid mods">
            <?php foreach ($mods as $mod): ?>
                <?php require BASE_PATH . '/app/Views/partials/mod-card.php'; ?>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-state">
            <strong>Bu kategori henuz bos.</strong>
            <p>Yayindaki modlar eklendiginde burada listelenecek.</p>
        </div>
    <?php endif; ?>
</section>
