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

$brand = $data['Brand'] ?? 'Retailer';
$subcategory = $data['subcategory'] ?? 'Products';
$updated = $data['updated'] ?? '';
$products = is_array($data['products'] ?? null) ? $data['products'] : [];
$pageTitle = 'STORE.com – ' . $subcategory . ' Offers';
$year = date('Y');
$winnerIndex = 0;
foreach ($products as $i => $product) {
    if (strcasecmp((string) ($product['best_for'] ?? ''), 'Best Overall') === 0) {
        $winnerIndex = $i;
        break;
    }
}

$offerPills = [];
foreach ($products as $product) {
    if (!empty($product['best_for'])) {
        $offerPills[] = $product['best_for'];
    }
    if (!empty($product['discount'])) {
        $offerPills[] = $product['discount'];
    }
}
$offerPills = array_slice(array_values(array_unique($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><?= promo_e($pageTitle) ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
</head>
<body class="bg-light-gradient">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <header class="header">
        <div class="container-fluid header-row">
            <a href="#" class="logo"><?= promo_e($brand) ?></a>
            <span class="header-pill"><?= promo_e($subcategory) ?></span>
        </div>
    </header>

    <div class="main-container custom-container">
        <section class="card border border-color radius-18 card-shadow bg-color-white p-4">
            <p class="sp-eyebrow text-xs"><?= $updated !== '' ? 'Updated ' . promo_e($updated) : 'Limited-time picks' ?> · <?= promo_e($brand) ?></p>
            <h1 class="sp-h1 heading-lg color-content fw-800">This week&#x27;s <?= promo_e(strtolower($subcategory)) ?> offers</h1>
            <p class="text-md color-muted">The top picks, framed by the deal that makes each one worth opening now — value, extras, and premium layouts from <?= promo_e($brand) ?>.</p>
            <?php if ($offerPills): ?>
                <div class="offer-pills">
                    <?php foreach ($offerPills as $pill): ?>
                        <span class="chip"><?= promo_e($pill) ?></span>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            <div class="row mt-3">
                <div class="col-xl-6 col-md-6 col-sm-12">
                    <a href="#quickPicks" class="button-style bg-color-white border border-color color-content text-sm fw-800">Shop Top Picks</a>
                </div>
            </div>
        </section>

        <section id="quickPicks" class="my-3">
            <?php
            $featured = $products[$winnerIndex] ?? null;
            $others = [];
            foreach ($products as $i => $product) {
                if ($i !== $winnerIndex) {
                    $others[] = $product;
                }
            }
            ?>
            <?php if ($featured):
                $name = $featured['name'] ?? 'Untitled product';
                $image = $featured['image_url'] ?? '';
                $url = $featured['retailer_url'] ?? '#';
                $bestFor = $featured['best_for'] ?? 'Top offer';
                $price = $featured['price'] ?? '';
                $listPrice = $featured['list_price'] ?? null;
                $rating = $featured['rating'] ?? '';
                $reviews = $featured['review_count'] ?? '';
                $lead = $featured['summary_short'] ?? ($featured['why_short'] ?? '');
            ?>
            <div class="bg-color-white border border-color radius-18 p-0 mb-3 shadow1-sm deal-card-wrap">
                <span class="deal-ribbon"><?= promo_e($bestFor) ?></span>
                <div class="product-img">
                    <?php if ($image !== ''): ?>
                        <img src="<?= promo_e($image) ?>" alt="<?= promo_e($name) ?>" loading="lazy">
                    <?php endif; ?>
                </div>
                <div class="product-body">
                    <p class="product-kicker text-color-green text-xs"><?= promo_e($bestFor) ?></p>
                    <h2 class="product-name"><?= promo_e($name) ?></h2>
                    <div class="quick-meta">
                        <?php if ($price !== ''): ?>
                            <span class="price-tag"><?= promo_e($price) ?><?php if (!empty($listPrice)): ?> <span class="old-price2"><?= promo_e($listPrice) ?></span><?php endif; ?></span>
                        <?php endif; ?>
                        <?php if ($rating !== '' || $reviews !== ''): ?>
                            <span class="rating-tag"><?= promo_e(trim($rating . ($rating !== '' ? '/5' : '') . ($reviews !== '' ? ' · ' . $reviews . ' reviews' : ''))) ?></span>
                        <?php endif; ?>
                    </div>
                    <?php if ($lead !== ''): ?>
                        <p class="text-sm color-muted my-3"><?= promo_e($lead) ?></p>
                    <?php endif; ?>
                    <a href="<?= promo_e($url) ?>" class="btn-amazon" target="_blank" rel="noopener noreferrer">View on <?= promo_e($brand) ?></a>
                </div>
            </div>
            <?php endif; ?>

            <?php if ($others): ?>
            <div class="row g-3">
                <?php foreach ($others as $product):
                    $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;
                    $rating = $product['rating'] ?? '';
                    $reviews = $product['review_count'] ?? '';
                    $lead = $product['summary_short'] ?? ($product['why_short'] ?? '');
                    $discount = $product['discount'] ?? null;
                ?>
                <div class="col-md-6">
                    <div class="bg-color-white border border-color radius-18 p-0 h-100 shadow1-sm d-flex justify-content-between flex-column deal-card-wrap">
                        <span class="deal-ribbon"><?= promo_e($bestFor) ?></span>
                        <div>
                            <div class="product-img">
                                <?php if ($image !== ''): ?>
                                    <img src="<?= promo_e($image) ?>" alt="<?= promo_e($name) ?>" loading="lazy">
                                <?php endif; ?>
                            </div>
                            <div class="product-body">
                                <p class="fw-700 mb-2 product-kicker text-color-green text-xs"><?= promo_e($bestFor) ?></p>
                                <h2 class="product-name"><?= promo_e($name) ?></h2>
                                <div class="quick-meta">
                                    <?php if ($price !== ''): ?>
                                        <span class="price-tag"><?= promo_e($price) ?><?php if (!empty($listPrice)): ?> <span class="old-price2"><?= promo_e($listPrice) ?></span><?php endif; ?></span>
                                    <?php endif; ?>
                                    <?php if ($rating !== '' || $reviews !== ''): ?>
                                        <span class="rating-tag"><?= promo_e(trim($rating . ($rating !== '' ? '/5' : '') . ($reviews !== '' ? ' · ' . $reviews . ' reviews' : ''))) ?></span>
                                    <?php endif; ?>
                                </div>
                                <?php if ($lead !== ''): ?>
                                    <p class="text-sm color-muted my-3"><?= promo_e($lead) ?></p>
                                <?php endif; ?>
                                <?php if (!empty($discount)): ?>
                                    <div class="price-note">✓ <?= promo_e($discount) ?></div>
                                <?php endif; ?>
                                <a href="<?= promo_e($url) ?>" class="btn-amazon" target="_blank" rel="noopener noreferrer">View on <?= promo_e($brand) ?></a>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </section>

        <div class="card border border-color radius-18 card-shadow bg-color-white p-md-4 p-3 mt-4">
            <p class="mb-2 text-md color-content fw-700">How this page is built</p>
            <p class="text-sm color-muted">store.com aggregates and summarizes published insights from multiple product review sources to help shoppers compare products faster. Pricing, ratings, and retailer details may change, so always confirm the latest information on the retailer page before buying.</p>
        </div>
    </div>

    <footer class="footer text-center">
        © <?= (int) $year ?> STORE.com · <a href="#" class="text-decoration-underline">Privacy policy</a> · <a href="#" class="text-decoration-underline"> Terms of Use </a>
    </footer>
    <script src="script.js"></script>
</body>
</html>
