<?php

declare(strict_types=1);

require __DIR__ . '/includes/cart.php';
require __DIR__ . '/includes/analytics.php';

$pdo = tap_storefront_pdo();
$salesPaused = tap_sales_paused($pdo);
tap_analytics_record($pdo, 'page_view', [
    'entity_type' => 'page',
    'entity_name' => 'Homepage',
]);
$products = tap_storefront_products($pdo);
$categories = tap_storefront_categories($products);
$cartCount = tap_cart_count();
$announcements = tap_homepage_announcements($pdo);
if ($salesPaused) {
    $announcements = ['The Aroma Pixies shop is taking a break. Previous purchases are unaffected by the pause.'];
}
$featuredProducts = array_slice(array_values(array_filter($products, static fn (array $product): bool => (string) ($product['image_url'] ?? '') !== '')), 0, 4);
if (count($featuredProducts) < 4) {
    $featuredProducts = array_slice($products, 0, 4);
}
$categoryCards = array_slice($categories, 0, 4);
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>The Aroma Pixies - Handmade Bath Treats and Digital Tools</title>
  <link rel="stylesheet" href="/assets/storefront.css?v=0.9.30">
</head>
<body>
  <?php echo tap_storefront_header($cartCount, $announcements); ?>

  <main>
    <section class="home-hero">
      <div class="home-hero-inner">
        <div class="home-hero-copy">
          <p class="eyebrow">Handmade · magickal · made with love</p>
          <h1>A little calm, comfort and sparkle for bath time.</h1>
          <p class="lede">Handmade bath treats, gentle self-care bits, and practical digital tools for makers, all from The Aroma Pixies.</p>
          <div class="home-actions">
            <a class="button" href="/shop.php"><?php echo $salesPaused ? 'Browse the catalogue' : 'Shop now'; ?></a>
            <?php if (!$salesPaused): ?><a class="button secondary" href="#newsletter-title">Get 10% off</a><?php endif; ?>
          </div>
        </div>
        <div class="home-hero-card">
          <img src="/assets/email/tap-newsletter-header.png" alt="The Aroma Pixies">
          <p>Soft, personal, handmade treats for cosy evenings, little rituals and useful maker moments.</p>
        </div>
      </div>
    </section>

    <div class="page home-page">
      <?php if ($salesPaused): ?>
        <p class="notice warn">The shop is not taking new orders at present. If you have already purchased a digital product, your existing download link still works within its original expiry and download limits. <a href="/contact.php">Contact us</a> if you need help.</p>
      <?php endif; ?>
      <?php if ($categoryCards): ?>
        <section class="home-section">
          <div class="section-heading">
            <p class="eyebrow">Browse the shelves</p>
            <h2><?php echo $salesPaused ? 'Browse by category' : 'Shop by category'; ?></h2>
          </div>
          <div class="home-category-grid">
            <?php foreach ($categoryCards as $category): ?>
              <a class="home-category-card" href="/shop.php?category=<?php echo rawurlencode((string) $category['key']); ?>">
                <span><?php echo tap_e((string) $category['label']); ?></span>
                <strong><?php echo tap_e((string) $category['count']); ?></strong>
              </a>
            <?php endforeach; ?>
          </div>
        </section>
      <?php endif; ?>

      <?php if ($featuredProducts): ?>
        <section class="home-section">
          <div class="section-heading section-heading-row">
            <div>
              <p class="eyebrow"><?php echo $salesPaused ? 'From the catalogue' : 'Ready to shop'; ?></p>
              <h2>Featured products</h2>
            </div>
            <a class="text-link" href="/shop.php">View all products</a>
          </div>
          <section class="product-grid home-featured-grid" aria-label="Featured products">
            <?php foreach ($featuredProducts as $product): ?>
              <article class="product-card">
                <?php if (($product['image_url'] ?? '') !== ''): ?>
                  <img src="<?php echo tap_e((string) $product['image_url']); ?>" alt="<?php echo tap_e((string) ($product['image_alt'] ?: $product['title'])); ?>">
                <?php endif; ?>
                <div class="product-card-body">
                  <div class="product-category-label"><?php echo tap_e(tap_storefront_product_category_label($product)); ?></div>
                  <h2><?php echo tap_e((string) $product['title']); ?></h2>
                  <div class="product-card-lower">
                    <div class="price"><?php echo tap_e(tap_storefront_price_range((string) $product['min_price'], (string) $product['max_price'])); ?></div>
                    <a class="button product-card-action" href="/products/<?php echo rawurlencode((string) $product['handle']); ?>">View product</a>
                  </div>
                </div>
              </article>
            <?php endforeach; ?>
          </section>
        </section>
      <?php endif; ?>

      <section class="home-story">
        <div>
          <p class="eyebrow">From Pixie HQ</p>
          <h2>Made with care, packed with personality.</h2>
        </div>
        <p>The Aroma Pixies is a small handmade business on the Norfolk Coast, creating bath and body treats with a soft, magical feel. You will also find digital products for makers tucked alongside the bath treats, because practical magic counts too.</p>
      </section>

      <?php if (!$salesPaused): ?><section class="newsletter-signup" aria-labelledby="newsletter-title">
        <div>
          <h2 id="newsletter-title">Sign up for our newsletter</h2>
          <p>Weekly-ish updates, new products, offers and useful notes from The Aroma Pixies. Sign up and get 10% off your next order. We promise not to spam your inbox.</p>
        </div>
        <form method="post" action="/newsletter-subscribe.php">
          <label for="newsletter_first_name">First name <span class="muted small">optional</span></label>
          <input id="newsletter_first_name" name="first_name" type="text" autocomplete="given-name">
          <label for="newsletter_email">Email address</label>
          <input id="newsletter_email" name="email" type="email" autocomplete="email" required>
          <button type="submit">Sign me up</button>
        </form>
      </section><?php endif; ?>
    </div>
  </main>

  <?php echo tap_storefront_footer(); ?>
  <?php echo tap_analytics_consent_banner(); ?>
</body>
</html>
