<?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_once __DIR__ . '/../app/helpers/cart.php';

$pageTitle = 'Search | LOVE-U Essentials';
$pageDescription = 'Search premium LOVE-U Essentials products.';

$query = trim($_GET['q'] ?? '');
$products = [];

if ($query !== '') {
    $pageTitle = 'Search results for ' . $query . ' | LOVE-U Essentials';
    $pageDescription = 'Search results for ' . $query . ' from LOVE-U Essentials.';

    $searchTerm = '%' . $query . '%';

    $stmt = $pdo->prepare("
        SELECT p.*, c.name AS category_name,
            (
                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
        LEFT JOIN categories c ON p.category_id = c.id
        WHERE p.status = 'active'
          AND (
                p.product_name LIKE ?
                OR p.short_description LIKE ?
                OR p.description LIKE ?
                OR p.scent_family LIKE ?
                OR c.name LIKE ?
          )
        ORDER BY p.is_featured DESC, p.is_best_seller DESC, p.id DESC
    ");

    $stmt->execute([
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm
    ]);

    $products = $stmt->fetchAll();
}

function render_search_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>

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

        <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';
?>

<section class="shop-hero">
    <div class="container">
        <div class="eyebrow">Search Products</div>

        <?php if ($query !== ''): ?>
            <h1>Search Results</h1>
            <p>Showing results for “<?= e($query); ?>”.</p>
        <?php else: ?>
            <h1>Search</h1>
            <p>Find your favourite LOVE-U Essentials products quickly.</p>
        <?php endif; ?>
    </div>
</section>

<section class="section">
    <div class="container">
        <form class="shop-search" method="GET" action="<?= e(BASE_URL); ?>/public/search.php" style="margin-bottom:34px;">
            <input
                type="search"
                name="q"
                placeholder="Search by product, category, scent..."
                value="<?= e($query); ?>"
                required
            >
            <button type="submit">Search</button>
        </form>

        <?php if ($query === ''): ?>
            <div class="empty-state">
                <h2>Start searching</h2>
                <p>Type a product name, category, or scent to begin.</p>
            </div>
        <?php elseif (!$products): ?>
            <div class="empty-state">
                <h2>No results found</h2>
                <p>No products matched “<?= e($query); ?>”. Try another keyword.</p>
                <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Browse Shop</a>
            </div>
        <?php else: ?>
            <div class="shop-topbar">
                <div class="shop-count">
                    <?= count($products); ?> result<?= count($products) === 1 ? '' : 's'; ?> found
                </div>

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

            <div class="product-grid">
                <?php foreach ($products as $product): ?>
                    <?php render_search_product_card($product); ?>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

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