<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';

$pageTitle = 'LOVE-U Essentials | Premium Self-Care Essentials';
$pageDescription = 'Discover premium LOVE-U Essentials products with secure checkout, luxury shopping experience, and UK & Grenada delivery-ready service.';

$featuredProducts = $pdo->query("
    SELECT p.*,
        (
            SELECT image_path
            FROM product_images
            WHERE product_id = p.id
            ORDER BY is_primary DESC, sort_order ASC, id ASC
            LIMIT 1
        ) AS primary_image
    FROM products p
    WHERE p.status = 'active' AND p.is_featured = 1
    ORDER BY p.id DESC
    LIMIT 4
")->fetchAll();

$bestSellers = $pdo->query("
    SELECT p.*,
        (
            SELECT image_path
            FROM product_images
            WHERE product_id = p.id
            ORDER BY is_primary DESC, sort_order ASC, id ASC
            LIMIT 1
        ) AS primary_image
    FROM products p
    WHERE p.status = 'active' AND p.is_best_seller = 1
    ORDER BY p.id DESC
    LIMIT 4
")->fetchAll();

$newArrivals = $pdo->query("
    SELECT p.*,
        (
            SELECT image_path
            FROM product_images
            WHERE product_id = p.id
            ORDER BY is_primary DESC, sort_order ASC, id ASC
            LIMIT 1
        ) AS primary_image
    FROM products p
    WHERE p.status = 'active' AND p.is_new_arrival = 1
    ORDER BY p.id DESC
    LIMIT 4
")->fetchAll();

function render_product_card($product)
{
    ?>
    <a href="<?= e(BASE_URL); ?>/public/product.php?slug=<?= e($product['slug']); ?>" class="product-card">
        <?php if (!empty($product['badge'])): ?>
            <div class="badge"><?= e($product['badge']); ?></div>
        <?php endif; ?>

        <div class="product-image">
            <?php if (!empty($product['primary_image'])): ?>
                <img src="<?= e(BASE_URL . '/' . $product['primary_image']); ?>" alt="<?= e($product['product_name']); ?>">
            <?php else: ?>
                <div class="product-placeholder"><?= e($product['product_name']); ?></div>
            <?php endif; ?>
        </div>

        <h3 class="product-title"><?= e($product['product_name']); ?></h3>

        <?php if (!empty($product['scent_family'])): ?>
            <div class="product-meta"><?= e($product['scent_family']); ?></div>
        <?php endif; ?>

        <div class="product-price">
            <?php if (!empty($product['sale_price'])): ?>
                <span class="old-price"><?= e(format_price($product['regular_price'], $product['currency_code'])); ?></span>
                <span class="sale-price"><?= e(format_price($product['sale_price'], $product['currency_code'])); ?></span>
            <?php else: ?>
                <?= e(format_price($product['regular_price'], $product['currency_code'])); ?>
            <?php endif; ?>
        </div>
    </a>
    <?php
}

require_once __DIR__ . '/includes/header.php';
?>
<?php if (isset($_GET['newsletter'])): ?>
    <?php
    $newsletterMessage = '';
    $newsletterType = 'success';

    if ($_GET['newsletter'] === 'success') {
        $newsletterMessage = 'Thank you for subscribing. Your LOVE-U Essentials updates are now active.';
    } elseif ($_GET['newsletter'] === 'resubscribed') {
        $newsletterMessage = 'Welcome back. Your newsletter subscription has been reactivated.';
    } elseif ($_GET['newsletter'] === 'exists') {
        $newsletterMessage = 'You are already subscribed to our newsletter.';
    } elseif ($_GET['newsletter'] === 'invalid_email') {
        $newsletterMessage = 'Please enter a valid email address.';
        $newsletterType = 'error';
    } elseif ($_GET['newsletter'] === 'invalid') {
        $newsletterMessage = 'Invalid request. Please try again.';
        $newsletterType = 'error';
    } else {
        $newsletterMessage = 'Newsletter subscription could not be completed. Please try again.';
        $newsletterType = 'error';
    }
    ?>

    <div class="newsletter-alert <?= $newsletterType === 'error' ? 'newsletter-alert-error' : ''; ?>">
        <?= e($newsletterMessage); ?>
    </div>
<?php endif; ?>
<section class="lue-hero">
    <div class="lue-hero-bg-orb lue-orb-one"></div>
    <div class="lue-hero-bg-orb lue-orb-two"></div>

    <div class="container lue-hero-inner">
        <div class="lue-hero-content reveal-up">
            <div class="eyebrow">Natural self-care essentials</div>

            <h1>Soft rituals for your body, your mood, and your everyday glow.</h1>

            <p>
                Discover LOVE-U Essentials — thoughtful body care, natural deodorants,
                soft accessories, and gift-ready self-care pieces made to feel gentle,
                beautiful, and personal.
            </p>

            <div class="hero-actions">
                <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Shop Collection</a>
                <a href="<?= e(BASE_URL); ?>/public/shop.php?filter=best-sellers" class="btn btn-light">Explore Best Sellers</a>
            </div>

            <div class="lue-hero-points">
                <span>Natural care</span>
                <span>Gift-ready</span>
                <span>Secure checkout</span>
            </div>
        </div>

        <div class="lue-hero-visual reveal-up reveal-delay-1">
            <div class="lue-hero-image-wrap">
                <img src="<?= e(BASE_URL); ?>/uploads/products/loveu-product-range.jpg" alt="LOVE-U Essentials natural self-care product range">
            </div>

            <div class="lue-hero-card lue-card-top">
                <span>New</span>
                <strong>Fresh everyday essentials</strong>
            </div>

            <div class="lue-hero-card lue-card-bottom">
                <span>LOVE10</span>
                <strong>First-order offer available</strong>
            </div>
        </div>
    </div>
</section>

<section class="lue-trust-row">
    <div class="container lue-trust-grid">
        <div class="reveal-up">
            <span>01</span>
            <strong>Body care made softer</strong>
            <p>Simple, nourishing essentials for daily routines.</p>
        </div>

        <div class="reveal-up reveal-delay-1">
            <span>02</span>
            <strong>Thoughtful gifting</strong>
            <p>Gift-ready checkout flows with a premium shopping feel.</p>
        </div>

        <div class="reveal-up reveal-delay-2">
            <span>03</span>
            <strong>Easy online ordering</strong>
            <p>Cart, wishlist, order tracking, and smooth checkout.</p>
        </div>
    </div>
</section>

<?php if ($featuredProducts): ?>
<section class="section lue-product-section">
    <div class="container">
        <div class="section-head reveal-up">
            <div>
                <div class="eyebrow">Featured Selection</div>
                <h2>Client-ready favourites</h2>
                <p>Selected LOVE-U Essentials products styled for a premium boutique shopping experience.</p>
            </div>

            <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn btn-light">Shop All</a>
        </div>

        <div class="product-grid lue-animated-grid">
            <?php foreach ($featuredProducts as $index => $product): ?>
                <div class="reveal-up reveal-delay-<?= e(min($index, 2)); ?>">
                    <?php render_product_card($product); ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
<?php endif; ?>

<section class="lue-story-section">
    <div class="container lue-story-grid">
        <div class="lue-story-image story-product-focus reveal-up">
    <img src="<?= e(BASE_URL); ?>/uploads/products/body-butter-resilience-wood.jpg" alt="LOVE-U body butter natural self-care">
</div>

        <div class="lue-story-content reveal-up reveal-delay-1">
            <div class="eyebrow">The LOVE-U feeling</div>
            <h2>Natural, warm, and beautifully simple.</h2>
            <p>
                LOVE-U Essentials is built around small self-care moments — the kind that make
                everyday routines feel softer, calmer, and more intentional.
            </p>

            <div class="lue-story-list">
                <span>Whipped body butter</span>
                <span>Natural deodorants</span>
                <span>Soft accessories</span>
                <span>Gift-ready essentials</span>
            </div>

            <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Discover Products</a>
        </div>
    </div>
</section>

<?php if ($bestSellers): ?>
<section class="section lue-product-section lue-product-section-alt">
    <div class="container">
        <div class="section-head reveal-up">
            <div>
                <div class="eyebrow">Best Sellers</div>
                <h2>Loved essentials</h2>
                <p>Products designed for repeat routines, gentle care, and everyday confidence.</p>
            </div>
        </div>

        <div class="product-grid lue-animated-grid">
            <?php foreach ($bestSellers as $index => $product): ?>
                <div class="reveal-up reveal-delay-<?= e(min($index, 2)); ?>">
                    <?php render_product_card($product); ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
<?php endif; ?>

<section class="lue-banner-cta">
    <div class="container">
        <div class="lue-banner-inner reveal-up">
            <div>
                <span class="footer-kicker">Gift-ready checkout</span>
                <h2>Send a little LOVE-U to someone special.</h2>
                <p>Built for thoughtful purchases, soft gifting, and smooth customer journeys.</p>
            </div>

            <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Start Shopping</a>
        </div>
    </div>
</section>

<?php if ($newArrivals): ?>
<section class="section lue-product-section">
    <div class="container">
        <div class="section-head reveal-up">
            <div>
                <div class="eyebrow">New Arrivals</div>
                <h2>Freshly added</h2>
                <p>Explore the newest LOVE-U Essentials pieces added to the collection.</p>
            </div>
        </div>

        <div class="product-grid lue-animated-grid">
            <?php foreach ($newArrivals as $index => $product): ?>
                <div class="reveal-up reveal-delay-<?= e(min($index, 2)); ?>">
                    <?php render_product_card($product); ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
<?php endif; ?>

<div class="discount-popup-overlay" id="discountPopup">
    <div class="discount-popup">
        <button type="button" class="discount-popup-close" onclick="closeDiscountPopup()">×</button>

        <span class="discount-popup-label">Exclusive Offer</span>

        <h2>Get 10% Off Your First Order</h2>

        <p>
            Join the LOVE-U List and receive self-care updates, product drops,
            and your first-order discount code.
        </p>

        <form method="POST" action="<?= e(BASE_URL); ?>/public/newsletter-submit.php" class="discount-popup-form">
            <?= csrf_field(); ?>

            <input
                type="text"
                name="newsletter_first_name"
                placeholder="First name"
            >

            <input
                type="email"
                name="newsletter_email"
                placeholder="Email address"
                required
            >

            <input type="hidden" name="newsletter_source" value="homepage_discount_popup">

            <button type="submit">Claim LOVE10</button>
        </form>

        <button type="button" class="discount-popup-skip" onclick="closeDiscountPopup()">
            No thanks, I’ll continue shopping
        </button>
    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>