<?php
require_once __DIR__ . '/config.php';

if (isset($_GET['query']) && !empty(trim($_GET['query']))) {
    $search_input = trim($_GET['query']);
    $search_term = '%' . $search_input . '%';
    
    try {
        // কিওয়ার্ডের সাথে মিল থাকা ৩-৫টি প্রোডাক্ট খোঁজা হচ্ছে
        $stmt = $pdo->prepare("SELECT id, name, price, discount_price, image FROM products WHERE status = 'active' AND name LIKE ? LIMIT 5");
        $stmt->execute([$search_term]);
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // যদি প্রোডাক্ট পাওয়া যায়
        if (!empty($results)) {
            foreach ($results as $product) {
                $price = number_format($product['discount_price'] > 0 ? $product['discount_price'] : $product['price']);
                $img_path = "uploads/products/" . $product['image'];
                if (empty($product['image']) || !file_exists($img_path)) {
                    $img_path = "https://via.placeholder.com/40";
                }
                
                echo '<a href="product_details.php?id=' . $product['id'] . '" class="search-suggest-item" style="display: flex; align-items: center; gap: 10px; padding: 10px; text-decoration: none; color: #333; border-bottom: 1px solid #f1f1f1;">';
                echo '<img src="' . $img_path . '" style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px;">';
                echo '<div style="flex: 1; min-width: 0;">';
                echo '<div style="font-size: 0.9rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">' . htmlspecialchars($product['name']) . '</div>';
                echo '<div style="font-size: 0.8rem; color: #ff416c; font-weight: bold;">৳' . $price . '</div>';
                echo '</div>';
                echo '</a>';
            }
        } else {
            // যদি কোনো প্রোডাক্ট ম্যাচ না করে, তাহলে "খুঁজে পাইনি" এবং ২-৩টি র্যান্ডম সাজেশন দেখাবে
            echo '<div style="padding: 12px; text-align: center; color: #777; font-size: 0.85rem; font-weight: bold;">';
            echo '<i class="fa-solid fa-magnifying-glass-blur" style="margin-right: 5px; color: #ccc;"></i> খুঁজে পাইনি! কিন্তু এগুলো দেখতে পারেন:';
            echo '</div>';
            
            $suggest_stmt = $pdo->query("SELECT id, name, price, discount_price, image FROM products WHERE status = 'active' ORDER BY RAND() LIMIT 3");
            $suggestions = $suggest_stmt->fetchAll(PDO::FETCH_ASSOC);
            
            foreach ($suggestions as $product) {
                $price = number_format($product['discount_price'] > 0 ? $product['discount_price'] : $product['price']);
                $img_path = "uploads/products/" . $product['image'];
                
                echo '<a href="product_details.php?id=' . $product['id'] . '" class="search-suggest-item" style="display: flex; align-items: center; gap: 10px; padding: 10px; text-decoration: none; color: #333; border-bottom: 1px solid #f1f1f1; background: #fafafa;">';
                echo '<img src="' . $img_path . '" style="width: 35px; height: 35px; object-fit: cover; border-radius: 4px; opacity: 0.8;">';
                echo '<div style="flex: 1; min-width: 0;">';
                echo '<div style="font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #555;">' . htmlspecialchars($product['name']) . '</div>';
                echo '<div style="font-size: 0.8rem; color: #666;">৳' . $price . '</div>';
                echo '</div>';
                echo '</a>';
            }
        }
    } catch (PDOException $e) {
        echo '<div style="padding: 10px; color: red;">Error loading results</div>';
    }
}
?>