<?php
declare(strict_types=1);

require_once __DIR__ . '/lib/tenant.php';
require_once __DIR__ . '/lib/media.php';
require_once __DIR__ . '/lib/products.php';

$tenant = resolve_tenant_from_host();
if (!$tenant) {
    http_response_code(404);
    echo 'Shop not found.';
    exit;
}
$tenantId = (int) $tenant['id'];
$fonts = font_pairing_css((string) $tenant['font_pairing']);
$tq = tenant_qs($tenant);

$ps = db()->prepare('SELECT business_name FROM page_settings WHERE tenant_id = ?');
$ps->execute([$tenantId]);
$business = shop_company_name($ps->fetch() ?: [], $tenant);

$stmt = db()->prepare(
    'SELECT * FROM products WHERE tenant_id = ? AND is_active = 1 ORDER BY display_order, id'
);
$stmt->execute([$tenantId]);
$products = $stmt->fetchAll();
cart_boot();
$cartCount = array_sum(array_map('intval', $_SESSION['cart'] ?? []));
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Shop · <?= htmlspecialchars($business) ?></title>
  <link href="<?= htmlspecialchars($fonts['import']) ?>" rel="stylesheet">
  <link rel="stylesheet" href="<?= APP_BASE ?>/assets/public.css">
  <link rel="stylesheet" href="<?= APP_BASE ?>/assets/shop.css">
  <style>:root{--primary:<?= htmlspecialchars((string)$tenant['primary_color']) ?>;--secondary:<?= htmlspecialchars((string)$tenant['secondary_color']) ?>;--font-heading:<?= $fonts['heading'] ?>;--font-body:<?= $fonts['body'] ?>;}</style>
</head>
<body>
<header class="site-header">
  <a class="logo" href="<?= APP_BASE ?>/<?= htmlspecialchars($tq) ?>"><?= htmlspecialchars($business) ?></a>
  <nav>
    <a href="<?= APP_BASE ?>/<?= htmlspecialchars($tq) ?>#styles">Styles</a>
    <a href="<?= APP_BASE ?>/shop.php<?= htmlspecialchars($tq) ?>">Shop</a>
    <a href="<?= APP_BASE ?>/client/login.php<?= htmlspecialchars($tq) ?>">My account</a>
    <a href="<?= APP_BASE ?>/cart.php<?= htmlspecialchars($tq) ?>" class="nav-cta">Cart (<?= (int) $cartCount ?>)</a>
  </nav>
</header>

<section class="section shop-hero">
  <div class="section-head">
    <h2>Shop the chair</h2>
    <p>Pomades, oils, and tools we use in the shop — Click &amp; Collect at your next visit.</p>
  </div>
  <div class="product-grid">
    <?php foreach ($products as $p):
      $img = media_public_url($p['image_path'] ?? null);
      $href = APP_BASE . '/product.php' . $tq . '&slug=' . urlencode((string) $p['slug']);
      // fix query string if tq already has ?
      $href = APP_BASE . '/product.php?' . http_build_query(array_filter([
        'tenant' => tenant_param($tenant),
        'slug' => $p['slug'],
      ]));
      ?>
      <article class="product-card">
        <a href="<?= htmlspecialchars($href) ?>">
          <?php if ($img): ?>
            <img src="<?= htmlspecialchars($img) ?>" alt="<?= htmlspecialchars($p['name']) ?>" loading="lazy">
          <?php else: ?>
            <div class="product-ph"><?= htmlspecialchars(strtoupper(substr($p['name'], 0, 1))) ?></div>
          <?php endif; ?>
          <div class="product-meta">
            <span class="pill"><?= htmlspecialchars((string) $p['category']) ?></span>
            <h3><?= htmlspecialchars($p['name']) ?></h3>
            <p><?= htmlspecialchars((string) $p['short_description']) ?></p>
            <strong>$<?= number_format((float) $p['price'], 2) ?></strong>
          </div>
        </a>
      </article>
    <?php endforeach; ?>
    <?php if (!$products): ?>
      <p class="muted">No products yet. Add some in Admin → Products.</p>
    <?php endif; ?>
  </div>
</section>
<footer class="site-footer sn-footer-only">
  <?php require_once __DIR__ . '/lib/sn_credit.php'; sn_credit_render(); ?>
</footer>
</body>
</html>
