<?php
// wunschliste.php

require_once __DIR__ . '/lib/bootstrap.php';

// 1) DB-Verbindung (einmal)
$conn = db_connect();

// Login-Check + Redirect MUSS vor jeglicher HTML-Ausgabe passieren
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Daten laden
$stmt = $conn->prepare("
    SELECT products.productID, products.model, products.description, products.imagePath
    FROM userFavorites INNER JOIN products ON userFavorites.productID = products.productID 
    WHERE userID = ?
");
$stmt->bind_param("i", $_SESSION['user_id']);
$stmt->execute();
$result = $stmt->get_result();
?>

<?php include 'header.php'; ?>

<?php if ($result->num_rows > 0): ?>
    <main>
    <section class="product-section">
        <h2>Deine Wunschliste</h2>
        <div class="wishlist-grid">
            <?php while ($product = $result->fetch_assoc()): ?>
                <?php $productId = (int)$product['productID']; ?>
                <a class="product-card wishlist-card" href="productpage.php?id=<?= $productId ?>">
                    <img
                            src="<?= isset($product['imagePath']) ? $product['imagePath'] : 'assets/images/placeholder.png' ?>"
                            alt="<?= htmlspecialchars($product['model']) ?>">

                    <div class="product-card__content">
                        <h3><?= htmlspecialchars($product['model']) ?></h3>
                        <p><?= htmlspecialchars($product['description']) ?></p>
                    </div>
                </a>
            <?php endwhile; ?>
        </div>
    </section>
    </main>
<?php else: ?>
    <main style="padding: 2rem 1rem; text-align: center; animation: fadeInUp 0.5s ease both;">
        <p style="color: var(--text-secondary); font-size: 1rem;">Deine Wunschliste ist noch leer.</p>
    </main>
<?php endif; ?>

<?php $stmt->close(); ?>


<?php include 'footer.php'; ?>