-- Seed price history with current prices as baseline data points
-- Run once to backfill initial data

-- Add a unique constraint if not exists (for ON CONFLICT)
CREATE UNIQUE INDEX IF NOT EXISTS idx_price_history_unique 
ON price_history (product_id, feed_id, recorded_at::date);

-- Insert today's prices as first data point
INSERT INTO price_history (product_id, price, feed_id, recorded_at)
SELECT po.product_id, MIN(po.price), po.feed_id, CURRENT_DATE
FROM product_offers po
WHERE po.in_stock = true
GROUP BY po.product_id, po.feed_id
ON CONFLICT DO NOTHING;

-- Insert a synthetic "yesterday" point with same prices (so charts have at least 2 points)
INSERT INTO price_history (product_id, price, feed_id, recorded_at)
SELECT po.product_id, MIN(po.price), po.feed_id, CURRENT_DATE - INTERVAL '1 day'
FROM product_offers po
WHERE po.in_stock = true
GROUP BY po.product_id, po.feed_id
ON CONFLICT DO NOTHING;

SELECT 'Seeded price history for ' || COUNT(DISTINCT product_id) || ' products' FROM price_history;
