<?php
// search.php
require 'config/db.php';


// Capture search query from GET
$search_query = isset($_GET['query']) ? trim($_GET['query']) : '';

try {
    if (!empty($search_query)) {
        // Search for matches in Title or Description, joined with Category
        $sql = "SELECT p.*, c.name as category_name 
                FROM products p 
                LEFT JOIN categories c ON p.category_id = c.id 
                WHERE (p.title LIKE :q OR p.description LIKE :q) 
                AND p.status = 'available' 
                ORDER BY p.created_at DESC";
        
        $stmt = $pdo->prepare($sql);
        // Use a wildcard search
        $stmt->execute(['q' => "%$search_query%"]);
        $results = $stmt->fetchAll();
    } else {
        // If empty, we could show recent items or leave it empty
        $results = [];
    }
} catch (PDOException $e) {
    die("Database Error.");
}

include 'includes/header.php'; 
?>

<style>
    .search-container { max-width: 1200px; margin: 40px auto; padding: 0 20px; font-family: 'Inter', sans-serif; }
    .search-header { margin-bottom: 40px; border-bottom: 1px solid var(--border); padding-bottom: 25px; }
    .search-header h2 { font-size: 26px; font-family: 'Syncopate', sans-serif; font-weight: 900; color: white; margin: 0; }
    .search-header span { color: var(--primary); }

    .results-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
        gap: 30px;
    }

    .product-card { 
        text-decoration: none; 
        color: inherit; 
        transition: transform 0.3s ease, box-shadow 0.3s ease;
    }
    
    .product-card:hover { 
        transform: translateY(-5px); 
    }

    .card-inner { 
        background: var(--nav-bg); 
        border: 1px solid var(--border); 
        border-radius: 15px; 
        overflow: hidden; 
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .image-wrapper {
        width: 100%;
        height: 180px;
        background: #0d1117;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
    }

    .image-wrapper img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    .card-content { padding: 20px; flex-grow: 1; display: flex; flex-direction: column; }
    .cat-tag { font-size: 10px; color: var(--primary); font-weight: 900; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; display: block; }
    .item-title { font-size: 16px; margin: 0 0 15px 0; color: white; line-height: 1.4; font-weight: 700; height: 45px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
    .item-price { font-size: 22px; font-weight: 900; color: white; margin-top: auto; font-family: 'Syncopate', sans-serif; }

    .no-results {
        text-align: center;
        padding: 100px 20px;
        color: var(--text-gray);
        grid-column: 1/-1;
    }
    .no-results i { font-size: 60px; margin-bottom: 25px; color: var(--border); }
    .no-results h3 { font-family: 'Syncopate', sans-serif; color: white; font-size: 18px; margin-bottom: 10px; }
</style>

<div class="search-container">
    <div class="search-header">
        <?php if(!empty($search_query)): ?>
            <h2>Results for: <span>"<?php echo htmlspecialchars($search_query); ?>"</span></h2>
            <p style="color: var(--text-gray); font-size: 12px; margin-top: 10px; font-weight: bold; text-transform: uppercase; letter-spacing: 1px;">
                <?php echo count($results); ?> items retrieved from the vault
            </p>
        <?php else: ?>
            <h2>Explore the <span>Vault</span></h2>
            <p style="color: var(--text-gray); font-size: 12px; margin-top: 10px;">Enter keywords to filter the digital marketplace.</p>
        <?php endif; ?>
    </div>

    <div class="results-grid">
        <?php if(!empty($results)): ?>
            <?php foreach($results as $item): ?>
                <a href="product_view.php?id=<?php echo $item['id']; ?>" class="product-card">
                    <div class="card-inner">
                        <div class="image-wrapper">
                            <?php if(!empty($item['image_url']) && $item['image_url'] !== 'default_item.png'): ?>
                                <img src="uploads/<?php echo $item['image_url']; ?>" alt="Product">
                            <?php else: ?>
                                <i class="fa-solid fa-box-open" style="font-size: 50px; color: #161b22;"></i>
                            <?php endif; ?>
                        </div>
                        <div class="card-content">
                            <span class="cat-tag"><?php echo htmlspecialchars($item['category_name'] ?? 'General'); ?></span>
                            <h3 class="item-title"><?php echo htmlspecialchars($item['title']); ?></h3>
                            <div class="item-price">$<?php echo number_format($item['price'], 2); ?></div>
                        </div>
                    </div>
                </a>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="no-results">
                <i class="fa-solid fa-ghost"></i>
                <h3>NO ASSETS FOUND</h3>
                <p>The vault is empty for this specific signature. Try broad search terms.</p>
                <a href="index.php" style="color: var(--primary); text-decoration: none; font-weight: 900; font-size: 12px; text-transform: uppercase; margin-top: 20px; display: inline-block; border: 1px solid var(--primary); padding: 10px 20px; border-radius: 8px;">
                    Return to Mainframe
                </a>
            </div>
        <?php endif; ?>
    </div>
</div>

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