<?php
$variation = 'offer';
/** Expects $data as already-decoded category JSON. */

if (!function_exists('cmp_e')) {
    function cmp_e($value): string
    {
        return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('cmp_price_float')) {
    function cmp_price_float($price): float
    {
        return (float) preg_replace('/[^\d.]/', '', (string) $price);
    }
}

$brand       = $data['Brand'] ?? 'Retailer';
$subcategory = $data['subcategory'] ?? 'Products';
$updated     = $data['updated'] ?? '';
$products    = is_array($data['products'] ?? null) ? $data['products'] : [];
$pageTitle   = 'rateit.com – ' . $subcategory . ' Offers';

$winnerIndex = 0;
$bestPrice = null;
foreach ($products as $i => $product) {
    if (strcasecmp((string) ($product['best_for'] ?? ''), 'Best Overall') === 0) {
        $winnerIndex = $i;
    }
    if (!empty($product['price'])) {
        $priceVal = cmp_price_float($product['price']);
        $bestPrice = $bestPrice === null ? $priceVal : min($bestPrice, $priceVal);
    }
}

$offerPills = [];
foreach ($products as $product) {
    if (!empty($product['best_for'])) {
        $offerPills[] = $product['best_for'];
    }
    if (!empty($product['discount'])) {
        $offerPills[] = $product['discount'];
    }
}
$offerPills = array_values(array_unique($offerPills));
$offerPills = array_slice($offerPills, 0, 4);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title><?= cmp_e($pageTitle) ?></title>
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,500;0,600;1,400&display=swap" />
  <link rel="stylesheet" href="style.css" />
</head>
<body class="promo-theme">
  <header class="topbar">
    <a href="../index.html" class="brand"><span class="brand-store"><?= cmp_e($brand) ?></span></a>
    <span class="state-pill"><?= cmp_e($subcategory) ?></span>
  </header>

  <main class="page-shell">
    <section class="promo-banner" aria-label="Promotional overview">
      <p class="eyebrow"><?= $updated !== '' ? 'Updated ' . cmp_e($updated) : 'Limited-time picks' ?> · <?= cmp_e($brand) ?></p>
      <h1>This week&#x27;s <?= cmp_e(strtolower($subcategory)) ?> offers</h1>
      <p>The top picks, framed by the deal that makes each one worth opening now — value, extras, and premium layouts from <?= cmp_e($brand) ?>.</p>
    </section>

    <?php if ($offerPills): ?>
      <div class="offer-pills" aria-hidden="true">
        <?php foreach ($offerPills as $pill): ?>
          <span><?= cmp_e($pill) ?></span>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>

    <section class="deal-list" aria-label="Promotional product offers">
      <?php foreach ($products as $index => $product):
          $rank = $index + 1;
          $isFeatured = $index === $winnerIndex;
          $name = $product['name'] ?? 'Untitled product';
          $image = $product['image_url'] ?? '';
          $url = $product['retailer_url'] ?? '#';
          $bestFor = $product['best_for'] ?? 'Offer';
          $price = $product['price'] ?? '';
          $listPrice = $product['list_price'] ?? null;
          $discount = $product['discount'] ?? null;
          $chips = is_array($product['chips'] ?? null) ? $product['chips'] : [];
          $copy = $product['summary_short'] ?? ($product['why_short'] ?? '');
          $isBestPrice = $price !== '' && $bestPrice !== null && cmp_price_float($price) === $bestPrice;
          $offerBits = array_filter([$price, $discount, $isBestPrice ? 'Lowest price' : null]);
          $urgency = $product['tradeoff_note'] ?? '';
      ?>
      <article class="deal-card<?= $isFeatured ? ' featured' : '' ?>">
        <span class="deal-ribbon"><?= cmp_e($bestFor) ?></span>
        <div class="deal-media">
          <span class="deal-rank"><?= (int) $rank ?></span>
          <a href="<?= cmp_e($url) ?>" target="_blank" rel="noopener noreferrer">
            <?php if ($image !== ''): ?>
              <img src="<?= cmp_e($image) ?>" alt="<?= cmp_e($name) ?>" />
            <?php endif; ?>
          </a>
        </div>
        <div>
          <?php if ($offerBits): ?>
            <p class="deal-offer"><?= cmp_e(implode(' · ', $offerBits)) ?></p>
          <?php endif; ?>
          <h2><?= cmp_e($name) ?></h2>
          <?php if ($copy !== ''): ?>
            <p><?= cmp_e($copy) ?></p>
          <?php endif; ?>
          <?php if ($chips): ?>
            <div class="deal-meta">
              <?php foreach ($chips as $chip): ?>
                <span><?= cmp_e($chip) ?></span>
              <?php endforeach; ?>
              <?php if (!empty($listPrice)): ?>
                <span class="list-strike"><?= cmp_e($listPrice) ?></span>
              <?php endif; ?>
            </div>
          <?php endif; ?>
          <a class="deal-cta" href="<?= cmp_e($url) ?>" target="_blank" rel="noopener noreferrer">Shop this offer at <?= cmp_e($brand) ?> →</a>
          <?php if ($urgency !== ''): ?>
            <p class="urgency"><?= cmp_e($urgency) ?></p>
          <?php endif; ?>
        </div>
      </article>
      <?php endforeach; ?>
    </section>
  </main>

  <footer class="site-footer">
    <p>© <?= date('Y') ?> <?= cmp_e($brand) ?></p>
  </footer>
</body>
</html>


