import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def scrape(driver, url):
    driver.get(url)

    try:
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'div.card__content'))
        )
        print("✅ Product grid loaded.")
    except:
        print("⚠️ Timed out waiting for products to load.")

    time.sleep(2)  # Give lazy-loaded images time to populate

    soup = BeautifulSoup(driver.page_source, 'html.parser')
    items = []

    for card in soup.select('div.card__content'):
        name_tag = card.select_one('.card__heading')
        price_tag = card.select_one('.price-item--regular')
        image_tag = card.find_previous('img')  # image often comes before content block in Shopify

        name = name_tag.get_text(strip=True) if name_tag else None
        price = price_tag.get_text(strip=True).replace("From ", "") if price_tag else None
        image = image_tag['src'] if image_tag and image_tag.has_attr('src') else None

        # Filter out names that contain 'bulk' (case-insensitive)
        if name and price and "bulk" not in name.lower():
            items.append({
                "name": name,
                "price": price,
                "image": image
            })

    return items