<?php
$variation = 'trust';
/** 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_reviews_int')) {
    function cmp_reviews_int($count): int
    {
        return (int) preg_replace('/[^\d]/', '', (string) $count);
    }
}

if (!function_exists('cmp_rating_float')) {
    function cmp_rating_float($rating): float
    {
        return (float) $rating;
    }
}

if (!function_exists('cmp_stars')) {
    function cmp_stars($rating): string
    {
        $filled = (int) round(min(5, max(0, (float) $rating)));
        return str_repeat('★', $filled) . str_repeat('☆', 5 - $filled);
    }
}

if (!function_exists('cmp_format_count')) {
    function cmp_format_count(int $count): string
    {
        return number_format($count);
    }
}

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

$totalReviews = 0;
$ratingSum = 0.0;
$ratedCount = 0;
$bestRating = 0.0;
$bestRatedName = '';
$mostReviewed = 0;

foreach ($products as $product) {
    $reviews = cmp_reviews_int($product['review_count'] ?? 0);
    $totalReviews += $reviews;
    $mostReviewed = max($mostReviewed, $reviews);
    if (($product['rating'] ?? '') !== '') {
        $rating = cmp_rating_float($product['rating']);
        $ratingSum += $rating;
        $ratedCount++;
        if ($rating >= $bestRating) {
            $bestRating = $rating;
            $bestRatedName = $product['brand'] ?? ($product['name'] ?? '');
        }
    }
}

$avgRating = $ratedCount > 0 ? number_format($ratingSum / $ratedCount, 1) : '—';
$trustBadges = [];
foreach ($products as $product) {
    if (!empty($product['certification'])) {
        $trustBadges[] = $product['certification'];
    }
}
if ($mostReviewed > 0) {
    $trustBadges[] = cmp_format_count($mostReviewed) . '+ reviews on a top pick';
}
$trustBadges[] = $brand . ' shopper ratings';
$trustBadges = array_values(array_unique($trustBadges));
$trustBadges = array_slice($trustBadges, 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="trust-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="trust-hero" aria-label="Social proof overview">
      <?php if ($updated !== ''): ?>
        <p class="eyebrow">Updated <?= cmp_e($updated) ?></p>
      <?php endif; ?>
      <h1>Rated, reviewed, and shopper-backed</h1>
      <p>These <?= cmp_e(strtolower($subcategory)) ?> earned their place from review volume, star averages, and shopper feedback at <?= cmp_e($brand) ?> — not from a single marketing claim.</p>
      <div class="proof-stats">
        <div class="proof-stat">
          <strong><?= cmp_e($totalReviews > 0 ? cmp_format_count($totalReviews) . '+' : '—') ?></strong>
          <span>Combined <?= cmp_e($brand) ?> reviews</span>
        </div>
        <div class="proof-stat">
          <strong><?= cmp_e($avgRating !== '—' ? $avgRating . '★' : '—') ?></strong>
          <span>Typical rating across the set</span>
        </div>
        <div class="proof-stat">
          <strong><?= $bestRating > 0 ? cmp_e(number_format($bestRating, 1) . '★') : '—' ?></strong>
          <span>Highest average<?= $bestRatedName !== '' ? ' (' . cmp_e($bestRatedName) . ')' : '' ?></span>
        </div>
      </div>
    </section>

    <?php if ($trustBadges): ?>
      <div class="trust-badges" aria-label="Trust signals">
        <?php foreach ($trustBadges as $badge): ?>
          <span><?= cmp_e($badge) ?></span>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>

    <section class="review-list" aria-label="Trusted product reviews">
      <?php foreach ($products as $index => $product):
          $rank = $index + 1;
          $name = $product['name'] ?? 'Untitled product';
          $image = $product['image_url'] ?? '';
          $url = $product['retailer_url'] ?? '#';
          $rating = $product['rating'] ?? '';
          $reviews = $product['review_count'] ?? '';
          $bestFor = $product['best_for'] ?? '';
          $chips = is_array($product['chips'] ?? null) ? $product['chips'] : [];
          $quote = $product['why_it_stands_out'] ?? ($product['summary_short'] ?? '');
          $quoteFooter = trim($brand . ' shoppers' . ($bestFor !== '' ? ' · ' . $bestFor : ''));
      ?>
      <article class="review-card">
        <div class="review-media">
          <span class="review-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>
          <p class="rating-line">
            <?php if ($rating !== ''): ?>
              <span class="stars" aria-hidden="true"><?= cmp_e(cmp_stars($rating)) ?></span>
              <span><?= cmp_e($rating) ?></span>
            <?php endif; ?>
            <?php if ($reviews !== ''): ?>
              <a href="<?= cmp_e($url) ?>" target="_blank" rel="noopener noreferrer"><?= cmp_e($reviews) ?> reviews ↗</a>
            <?php endif; ?>
          </p>
          <h2><?= cmp_e($name) ?></h2>
          <?php if ($quote !== ''): ?>
            <blockquote class="quote">
              <?= cmp_e($quote) ?>
              <footer><?= cmp_e($quoteFooter) ?></footer>
            </blockquote>
          <?php endif; ?>
          <?php if ($chips): ?>
            <div class="proof-chips">
              <?php foreach ($chips as $chip): ?>
                <span><?= cmp_e($chip) ?></span>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>
          <a class="review-cta" href="<?= cmp_e($url) ?>" target="_blank" rel="noopener noreferrer">Read reviews &amp; shop at <?= cmp_e($brand) ?> →</a>
        </div>
      </article>
      <?php endforeach; ?>
    </section>
  </main>

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


