<?php

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

require_customer();

$pageTitle = 'Wishlist | LOVE-U Essentials';
$pageDescription = 'View and manage your LOVE-U Essentials wishlist.';

$currentCustomer = customer();

if (isset($_GET['add'])) {
    $productId = (int) $_GET['add'];

    if ($productId > 0) {
        $stmt = $pdo->prepare("
            SELECT id
            FROM products
            WHERE id = ? AND status = 'active'
            LIMIT 1
        ");
        $stmt->execute([$productId]);
        $product = $stmt->fetch();

        if ($product) {
            $stmt = $pdo->prepare("
                SELECT id
                FROM wishlist_items
                WHERE customer_id = ? AND product_id = ?
                LIMIT 1
            ");
            $stmt->execute([$currentCustomer['id'], $productId]);

            if (!$stmt->fetch()) {
                $stmt = $pdo->prepare("
                    INSERT INTO wishlist_items (customer_id, product_id)
                    VALUES (?, ?)
                ");
                $stmt->execute([$currentCustomer['id'], $productId]);
            }
        }
    }

    redirect(BASE_URL . '/public/wishlist.php?added=1');
}

if (isset($_GET['remove'])) {
    $productId = (int) $_GET['remove'];

    if ($productId > 0) {
        $stmt = $pdo->prepare("
            DELETE FROM wishlist_items
            WHERE customer_id = ? AND product_id = ?
        ");
        $stmt->execute([$currentCustomer['id'], $productId]);
    }

    redirect(BASE_URL . '/public/wishlist.php?removed=1');
}

$stmt = $pdo->prepare("
    SELECT wi.created_at, 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,
        (
            SELECT COUNT(*)
            FROM product_variations pv
            WHERE pv.product_id = p.id
              AND pv.status = 'active'
        ) AS variation_count
    FROM wishlist_items wi
    INNER JOIN products p ON wi.product_id = p.id
    WHERE wi.customer_id = ?
      AND p.status = 'active'
    ORDER BY wi.id DESC
");
$stmt->execute([$currentCustomer['id']]);
$wishlistItems = $stmt->fetchAll();

require_once __DIR__ . '/includes/header.php';
?>

<section class="lue-wishlist-page">
    <div class="lue-wishlist-orb lue-wishlist-orb-one"></div>
    <div class="lue-wishlist-orb lue-wishlist-orb-two"></div>

    <div class="container">
        <div class="lue-wishlist-hero">
            <div>
                <div class="eyebrow">My Wishlist</div>
                <h1>Saved Favourites</h1>
                <p>Keep your favourite LOVE-U Essentials in one place and return whenever you’re ready to shop.</p>
            </div>

            <a href="<?= e(BASE_URL); ?>/public/shop.php" class="lue-wishlist-shop-link">
                Continue Shopping
            </a>
        </div>

        <?php if (isset($_GET['added'])): ?>
            <div class="lue-wishlist-alert lue-wishlist-alert-success">
                Product added to wishlist.
            </div>
        <?php endif; ?>

        <?php if (isset($_GET['removed'])): ?>
            <div class="lue-wishlist-alert">
                Product removed from wishlist.
            </div>
        <?php endif; ?>

        <?php if (!$wishlistItems): ?>
            <div class="lue-wishlist-empty">
                <span>Empty Wishlist</span>
                <h2>Your wishlist is waiting.</h2>
                <p>Browse the shop and save the LOVE-U products you want to revisit later.</p>
                <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Shop Now</a>
            </div>
        <?php else: ?>
            <div class="lue-wishlist-toolbar">
                <div>
                    <span>Saved Items</span>
                    <strong><?= e(count($wishlistItems)); ?> product<?= count($wishlistItems) === 1 ? '' : 's'; ?></strong>
                </div>

                <p>Review, remove, or move your favourites to cart.</p>
            </div>

            <div class="lue-wishlist-grid-page">
                <?php foreach ($wishlistItems as $product): ?>
                    <?php
                    $hasVariations = (int) ($product['variation_count'] ?? 0) > 0;
                    ?>

                    <article class="lue-wishlist-product-card">
                        <?php if (!empty($product['badge'])): ?>
                            <div class="badge"><?= e($product['badge']); ?></div>
                        <?php endif; ?>

                        <a href="<?= e(BASE_URL); ?>/public/product.php?slug=<?= e($product['slug']); ?>" class="lue-wishlist-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; ?>
                        </a>

                        <div class="lue-wishlist-product-body">
                            <div class="lue-wishlist-product-meta">
                                <span>Saved <?= e(date('d M Y', strtotime($product['created_at']))); ?></span>

                                <?php if ($hasVariations): ?>
                                    <strong>Options available</strong>
                                <?php else: ?>
                                    <strong>Ready to cart</strong>
                                <?php endif; ?>
                            </div>

                            <h3>
                                <a href="<?= e(BASE_URL); ?>/public/product.php?slug=<?= e($product['slug']); ?>">
                                    <?= e($product['product_name']); ?>
                                </a>
                            </h3>

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

                            <div class="lue-wishlist-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>

                            <div class="lue-wishlist-actions">
                                <?php if ($hasVariations): ?>
                                    <a href="<?= e(BASE_URL); ?>/public/product.php?slug=<?= e($product['slug']); ?>" class="btn">
                                        View Options
                                    </a>
                                <?php else: ?>
                                    <form method="POST" action="<?= e(BASE_URL); ?>/public/cart.php">
                                        <input type="hidden" name="action" value="add">
                                        <input type="hidden" name="product_id" value="<?= e($product['id']); ?>">
                                        <input type="hidden" name="quantity" value="1">
                                        <button type="submit" class="btn">Add to Cart</button>
                                    </form>
                                <?php endif; ?>

                                <a
                                    href="<?= e(BASE_URL); ?>/public/wishlist.php?remove=<?= e($product['id']); ?>"
                                    class="lue-wishlist-remove"
                                >
                                    Remove
                                </a>
                            </div>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

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