
// --- BROWSE FEED LOGIC (Restored & Fixed) ---
window.renderBrowseFeed = function (category = 'All') {
    const container = document.getElementById('browse-feed');
    if (!container) return;
    container.innerHTML = '';

    const allListings = (typeof getListings === 'function') ? getListings() : (window.SERVER_CACHE || []);

    // Filter
    const filtered = allListings.filter(item => {
        if (!item.title) return false;
        // Text Filter
        if (category !== 'All' && category !== '' && item.category !== category) {
            // Simple string partial match if category is broad? 
            // For now assume exact match or 'All'
            if (!item.category || !item.category.includes(category)) return false;
        }
        return true;
    });

    if (filtered.length === 0) {
        container.innerHTML = `
            <div class="col-span-2 flex flex-col items-center justify-center py-20 text-gray-400">
                <span class="material-symbols-outlined text-6xl mb-4">search_off</span>
                <p>No items found in ${category}.</p>
            </div>
        `;
        return;
    }

    filtered.forEach(item => {
        const img = (item.images && item.images[0]) ? item.images[0] : (item.image || 'https://placehold.co/150');
        const price = item.price ? '₦' + item.price.toLocaleString() : 'Contact';

        // NO CART BUTTON HERE - Just Open Details
        const el = document.createElement('div');
        el.className = "bg-white p-3 rounded-2xl shadow-sm border border-gray-100 flex flex-col gap-2";
        el.onclick = () => openProductModal(item);

        el.innerHTML = `
            <div class="aspect-square bg-gray-100 rounded-xl bg-cover bg-center relative" style="background-image: url('${img}')">
                ${item.type === 'buy' ? '<span class="absolute top-2 left-2 bg-blue-500 text-white text-[9px] font-bold px-2 py-0.5 rounded-full">REQUEST</span>' : ''}
            </div>
            <div>
                <h4 class="font-bold text-gray-900 text-sm truncate">${item.title}</h4>
                <div class="flex justify-between items-center mt-1">
                    <span class="text-primary font-bold text-xs">${price}</span>
                    <div class="flex gap-1">
                        <!-- Favorites Only -->
                         <button onclick="event.stopPropagation(); toggleFavorite('${item.id}', this)" class="w-8 h-8 rounded-full bg-gray-50 flex items-center justify-center text-gray-400 hover:text-red-500 hover:bg-red-50 transition">
                            <span class="material-symbols-outlined text-[16px]">favorite</span>
                        </button>
                    </div>
                </div>
            </div>
        `;
        container.appendChild(el);
    });
};

// --- FIX: Robust Request Deal ---
window.requestDeal = function () {
    // 1. Try Active Ride (Item Sheet)
    if (activeRide && activeRide.item) {
        openChatOptions(activeRide.item.id);
        return;
    }

    // 2. Try Selected Pin (Leaflet Selection)
    // If logic works, activeRide.item SHOULD stem from openItemSheet. 
    // If user clicked "Start Deal" but sheet was closed (unlikely) or bug?

    // 3. Fallback
    showMessage("Select Item", "Please select a product on the map first.", "info");
};
