<?php
use Core\System\ThemeManager;

$siteName = ThemeManager::getSetting('site_name', 'Website');
$archiveTitle = $archive_title ?? __('products.archive_title', 'Products');
$archiveDescription = $archive_description ?? __('products.archive_description', 'Browse our latest products.');
$products = $products ?? [];
$pagination = $pagination ?? ['page' => 1, 'last_page' => 1];
$canonicalBase = $canonicalBase ?? '/products';
$searchQuery = $search_query ?? '';
?>
<section class="py-5">
    <div class="container">
        <header class="mb-4 text-center">
            <h1><?= htmlspecialchars($archiveTitle, ENT_QUOTES, 'UTF-8') ?></h1>
            <p class="text-muted mb-0"><?= htmlspecialchars($archiveDescription, ENT_QUOTES, 'UTF-8') ?></p>
        </header>

        <?php if (empty($products)): ?>
            <div class="alert alert-light border text-center"><?= __('products.no_products_title', 'No products yet') ?></div>
        <?php else: ?>
            <div class="row g-4">
                <?php foreach ($products as $product): ?>
                    <div class="col-6 col-md-4 col-lg-3">
                        <article class="card h-100 border-0 shadow-sm">
                            <a href="/product/<?= htmlspecialchars($product['slug'] ?? (string)(int)$product['id'], ENT_QUOTES, 'UTF-8') ?>" class="text-decoration-none text-dark">
                                <img src="<?= htmlspecialchars($product['featured_image'] ?? '/themes/matjari/assets/img/placeholder.png', ENT_QUOTES, 'UTF-8') ?>" class="card-img-top" alt="<?= htmlspecialchars($product['title'] ?? $archiveTitle, ENT_QUOTES, 'UTF-8') ?>">
                                <div class="card-body">
                                    <h2 class="h6 mb-2"><?= htmlspecialchars($product['title'] ?? '', ENT_QUOTES, 'UTF-8') ?></h2>
                                    <?php if (!empty($product['short_description'])): ?>
                                        <p class="small text-muted mb-2"><?= htmlspecialchars(mb_substr(trim(strip_tags((string)$product['short_description'])), 0, 90), ENT_QUOTES, 'UTF-8') ?></p>
                                    <?php endif; ?>
                                    <div class="fw-bold"><?= htmlspecialchars((string)($product['sale_price'] ?: $product['price'] ?? ''), ENT_QUOTES, 'UTF-8') ?></div>
                                </div>
                            </a>
                        </article>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <?php if (($pagination['last_page'] ?? 1) > 1): ?>
            <nav class="mt-4" aria-label="Pagination">
                <ul class="pagination justify-content-center">
                    <?php for ($i = 1; $i <= (int)$pagination['last_page']; $i++): ?>
                        <li class="page-item <?= $i === (int)$pagination['page'] ? 'active' : '' ?>">
                            <a class="page-link" href="<?= htmlspecialchars($canonicalBase . ($i > 1 ? (str_contains($canonicalBase, '?') ? '&page=' . $i : '?page=' . $i) : ''), ENT_QUOTES, 'UTF-8') ?>"><?= $i ?></a>
                        </li>
                    <?php endfor; ?>
                </ul>
            </nav>
        <?php endif; ?>
    </div>
</section>
