-- Get 5 sample image URLs per feed
WITH ranked AS (
  SELECT f.merchant_name, f.id as feed_id, po.image_url,
    ROW_NUMBER() OVER (PARTITION BY f.id ORDER BY random()) as rn
  FROM feeds f
  JOIN product_offers po ON po.feed_id = f.id
  WHERE po.image_url IS NOT NULL AND po.image_url != ''
)
SELECT merchant_name, feed_id, image_url
FROM ranked
WHERE rn <= 5
ORDER BY feed_id, rn;
