SELECT p.id, LEFT(p.name, 40) as name,
  CASE WHEN p.cutout_image_url IS NOT NULL THEN 'cutout' WHEN p.image_url IS NOT NULL THEN 'image' ELSE 'none' END as img_type,
  LEFT(COALESCE(p.cutout_image_url, p.image_url, 'NULL'), 80) as url
FROM products p
JOIN product_offers po ON po.product_id = p.id AND po.in_stock = true
WHERE p.is_sneaker = true AND p.image_url IS NOT NULL AND p.image_url != ''
GROUP BY p.id
ORDER BY COUNT(po.id) DESC, p.clicks_7d DESC NULLS LAST
LIMIT 8;
