<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';

$cat = trim((string) ($_GET['cat'] ?? ''));
$q = trim((string) ($_GET['q'] ?? ''));
$products = store_read('products');

$products = array_values(array_filter($products, static function (array $p) use ($cat, $q): bool {
    if (($p['status'] ?? 'active') !== 'active') {
        return false;
    }
    if ($cat !== '' && product_category_root_slug($p) !== $cat) {
        return false;
    }
    if ($q !== '') {
        $hay = mb_strtolower(($p['title'] ?? '') . ' ' . ($p['description'] ?? '') . ' ' . ($p['brand'] ?? '') . ' ' . ($p['seller_name'] ?? '') . ' ' . ($p['seller_username'] ?? ''));
        if (!str_contains($hay, mb_strtolower($q))) {
            return false;
        }
    }
    return true;
}));

usort($products, static fn(array $a, array $b): int => strcmp($b['created_at'] ?? '', $a['created_at'] ?? ''));

$pageTitle = 'Vitrin';
$pageClass = 'page-home';
require __DIR__ . '/includes/header.php';
require __DIR__ . '/includes/app_header.php';
?>

<section class="product-grid">
    <?php if (!$products): ?>
        <div class="empty"><?= e(theme_t('home_empty')) ?></div>
    <?php endif; ?>
    <?php foreach ($products as $p):
        $seller = product_seller($p);
        $username = $seller ? seller_username($seller, (string) ($seller['name'] ?? '')) : '';
        $rating = (float) ($seller['rating'] ?? ($p['seller_rating'] ?? 5));
        $reviews = (int) ($seller['review_count'] ?? ($p['seller_reviews'] ?? 0));
        $shopUrl = url('magaza.php' . (!empty($p['seller_id']) ? '?s=' . rawurlencode((string) $p['seller_id']) : ''));
        $labels = product_card_labels($p);
        $likes = product_likes($p);
        $comments = comments_count_for_product((string) ($p['id'] ?? ''));
        $detailUrl = url('urun.php?id=' . rawurlencode((string) $p['id']));
        ?>
        <article class="product-card">
            <?php if ($seller && $username !== ''): ?>
                <a class="pc-seller" href="<?= e($shopUrl) ?>">
                    <?= seller_avatar_html($seller, 'avatar sq tiny', '@' . $username) ?>
                    <div class="pc-seller-meta">
                        <strong><?= e($username) ?></strong>
                        <div class="seller-line">
                            <span class="stars"><?= e(seller_stars_text($rating)) ?></span>
                            <span class="hint">(<?= (int) $reviews ?>)</span>
                        </div>
                    </div>
                    <span class="shop-follow-icon tiny" title="Şimdilik pasif" aria-hidden="true"><?= follow_icon_svg(16) ?></span>
                </a>
            <?php else: ?>
                <div class="pc-seller is-empty">
                    <div class="avatar sq tiny" aria-hidden="true"><span>?</span></div>
                    <div class="pc-seller-meta">
                        <strong>Satıcı seçilmedi</strong>
                        <div class="seller-line"><span class="hint">Ürünü düzenleyip satıcı ata</span></div>
                    </div>
                </div>
            <?php endif; ?>

            <a class="product-media" href="<?= e($detailUrl) ?>">
                <img src="<?= e(product_image_url($p)) ?>" alt="<?= e($p['title'] ?? '') ?>" loading="lazy">
                <?= product_shipping_badge($p) ?>
            </a>

            <div class="pc-foot">
                <a class="pc-info" href="<?= e($detailUrl) ?>">
                    <strong><?= e($labels['brand']) ?></strong>
                    <?php if ($labels['sub'] !== ''): ?>
                        <span class="pc-sub"><?= e($labels['sub']) ?></span>
                    <?php endif; ?>
                    <em class="pc-price"><?= e(money($p['price'] ?? 0)) ?></em>
                </a>
                <div class="pc-social" aria-label="Beğeni ve yorum">
                    <span class="pc-like">
                        <?= heart_icon_svg(16) ?>
                        <span class="pc-num"><?= e(number_format($likes, 0, ',', '.')) ?></span>
                    </span>
                    <span class="pc-sep" aria-hidden="true"></span>
                    <span class="pc-comment">
                        <?= comment_icon_svg(16) ?>
                        <span class="pc-num"><?= e(number_format($comments, 0, ',', '.')) ?></span>
                    </span>
                </div>
            </div>
        </article>
    <?php endforeach; ?>
</section>

<?php require __DIR__ . '/includes/footer.php'; ?>
