#!/bin/bash
echo "=== SneakerPicks Nightly Sync started at $(date) ==="
cd /var/www/sneakerpicks

# Snapshot current prices BEFORE sync
echo "=== Snapshotting prices ==="
docker exec -i supabase-db psql -U supabase_admin -d sneakerpicks << 'PRICE_SQL'
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;
SELECT 'Price snapshots recorded: ' || COUNT(DISTINCT product_id) FROM price_history WHERE recorded_at::date = CURRENT_DATE;
PRICE_SQL

python3 scripts/feed-sync-v4.py 2>&1

# Re-apply is_sneaker filter + name cleanup for new products
docker exec -i supabase-db psql -U supabase_admin -d sneakerpicks << 'SQL'
-- Count before cleanup
SELECT 'Sneakers BEFORE cleanup: ' || COUNT(*) FROM products WHERE is_sneaker = true;

UPDATE products SET is_sneaker = true WHERE is_sneaker IS NULL;
UPDATE products SET is_sneaker = false WHERE is_sneaker = true AND google_product_category IS NOT NULL AND google_product_category != '187';
UPDATE products SET is_sneaker = false WHERE is_sneaker = true AND (
  -- Non-sneaker footwear
  LOWER(name) LIKE '%laars%' OR LOWER(name) LIKE '%boot%' OR LOWER(name) LIKE '%sandal%'
  OR LOWER(name) LIKE '%muiltje%' OR LOWER(name) LIKE '%pumps%' OR LOWER(name) LIKE '%hak%'
  OR LOWER(name) LIKE '%ballerina%' OR LOWER(name) LIKE '%mocassin%' OR LOWER(name) LIKE '%loafer%'
  OR LOWER(name) LIKE '%slipper%' OR LOWER(name) LIKE '%pantoffel%' OR LOWER(name) LIKE '%clog%'
  OR LOWER(name) LIKE '%espadrille%' OR LOWER(name) LIKE '%cowboy%'
  OR LOWER(name) LIKE '%instapper%' OR LOWER(name) LIKE '%laarzen%'
  -- Clothing
  OR LOWER(name) LIKE '%sportjack%' OR LOWER(name) LIKE '%regenjack%' OR LOWER(name) LIKE '%windjack%'
  OR LOWER(name) LIKE '%bodysuit%' OR LOWER(name) LIKE '%skort%' OR LOWER(name) LIKE '%track top%'
  OR LOWER(name) LIKE '%short%' OR LOWER(name) LIKE '%jurk%' OR LOWER(name) LIKE '%dress%'
  OR LOWER(name) LIKE '%jogging%' OR LOWER(name) LIKE '%pant %' OR LOWER(name) LIKE '%pants%'
  OR LOWER(name) LIKE '%tight%' OR LOWER(name) LIKE '%beha%' OR LOWER(name) LIKE '%tank %'
  OR LOWER(name) LIKE '%longsleeve%' OR LOWER(name) LIKE '%mouw%' OR LOWER(name) LIKE '%skirt%'
  OR LOWER(name) LIKE '%shirt%' OR LOWER(name) LIKE '%hoodie%' OR LOWER(name) LIKE '%sweater%'
  OR LOWER(name) LIKE '%trui%' OR LOWER(name) LIKE '%trainingspak%' OR LOWER(name) LIKE '%replica%'
  OR LOWER(name) LIKE '%jersey%' OR LOWER(name) LIKE '%jacket%'
  OR LOWER(name) LIKE '%voetbalschoen%' OR LOWER(name) LIKE '%handbalschoen%'
  OR LOWER(name) LIKE '%jack %'
  -- Accessories
  OR LOWER(name) LIKE '%rugzak%' OR LOWER(name) LIKE '%sokken%' OR LOWER(name) LIKE '%sokkken%'
  OR LOWER(name) LIKE '% tas %' OR LOWER(name) LIKE '% tas' OR LOWER(name) LIKE '%backpack%'
  OR LOWER(name) LIKE '%veters%' OR LOWER(name) LIKE '%veter %'
  OR LOWER(name) LIKE '%inlegzool%' OR LOWER(name) LIKE '%inlegzolen%' OR LOWER(name) LIKE '%insole%'
  OR LOWER(name) LIKE '%schoenlepel%' OR LOWER(name) LIKE '%shoehorn%'
  OR LOWER(name) LIKE '%spray%' OR LOWER(name) LIKE '%crème%' OR LOWER(name) LIKE '%creme%'
  OR LOWER(name) LIKE '%borstel%' OR LOWER(name) LIKE '%brush%'
  OR LOWER(name) LIKE '%pet %' OR LOWER(name) LIKE '%cap %' OR LOWER(name) LIKE '%muts%'
  OR LOWER(name) LIKE '%handschoen%' OR LOWER(name) LIKE '%riem%' OR LOWER(name) LIKE '%belt%'
  OR LOWER(name) LIKE '%portemonnee%' OR LOWER(name) LIKE '%wallet%'
  OR LOWER(name) LIKE '%accessoire%' OR LOWER(name) LIKE '%zonnebril%' OR LOWER(name) LIKE '%sunglasses%'
  OR LOWER(name) LIKE '%horloge%' OR LOWER(name) LIKE '%watch %'
  OR LOWER(name) LIKE '%parfum%' OR LOWER(name) LIKE '%fragrance%' OR LOWER(name) LIKE '%deodorant%'
  OR LOWER(name) LIKE '%sleutelhanger%' OR LOWER(name) LIKE '%keychain%'
  OR LOWER(name) LIKE '%telefoonhoesje%' OR LOWER(name) LIKE '%phone case%'
  -- Shoe care
  OR LOWER(name) LIKE '%schoenverzorging%' OR LOWER(name) LIKE '%shoe care%'
  OR LOWER(name) LIKE '%schoenpoets%' OR LOWER(name) LIKE '%cleaner%'
  OR LOWER(name) LIKE '%impregneer%' OR LOWER(name) LIKE '%protector%'
  OR LOWER(name) LIKE '%schoenenrek%' OR LOWER(name) LIKE '%schoenspanner%'
);
UPDATE products SET is_sneaker = false WHERE is_sneaker = true AND google_product_category IS NULL
  AND LOWER(name) NOT LIKE '%sneaker%' AND LOWER(name) NOT LIKE '%trainer%'
  AND LOWER(name) NOT LIKE '%schoen%' AND LOWER(name) NOT LIKE '%shoe%'
  AND LOWER(name) NOT LIKE '%running%' AND LOWER(name) NOT LIKE '%air max%'
  AND LOWER(name) NOT LIKE '%air force%' AND LOWER(name) NOT LIKE '%dunk%'
  AND LOWER(name) NOT LIKE '%jordan%' AND LOWER(name) NOT LIKE '%gel-%'
  AND LOWER(name) NOT LIKE '%ultraboost%' AND LOWER(name) NOT LIKE '%superstar%'
  AND LOWER(name) NOT LIKE '%stan smith%' AND LOWER(name) NOT LIKE '%gazelle%'
  AND LOWER(name) NOT LIKE '%samba%' AND LOWER(name) NOT LIKE '%campus%'
  AND LOWER(name) NOT LIKE '%spezial%' AND LOWER(name) NOT LIKE '%handball%'
  AND LOWER(name) NOT LIKE '%old skool%' AND LOWER(name) NOT LIKE '%chuck taylor%'
  AND LOWER(name) NOT LIKE '%all star%' AND LOWER(name) NOT LIKE '%574%'
  AND LOWER(name) NOT LIKE '%990%' AND LOWER(name) NOT LIKE '%fresh foam%'
  AND LOWER(name) NOT LIKE '%blazer%' AND LOWER(name) NOT LIKE '%cortez%'
  AND LOWER(name) NOT LIKE '%pegasus%' AND LOWER(name) NOT LIKE '%cloud%'
  AND LOWER(name) NOT LIKE '%hoka%' AND LOWER(name) NOT LIKE '%runner%'
  AND LOWER(name) NOT LIKE '%retro%' AND LOWER(name) NOT LIKE '%court %'
  AND LOWER(name) NOT LIKE '%boost%' AND LOWER(name) NOT LIKE '%slide%'
  AND LOWER(name) NOT LIKE '%skate%' AND LOWER(name) NOT LIKE '%hardloop%';
UPDATE products SET name = regexp_replace(name, ' - US[WM]?[0-9]+[.,][0-9],EU[0-9]+[.,][0-9],UK[0-9]+[.,][0-9]$', '')
  WHERE name ~ '- US[WM]?[0-9]+[.,][0-9],EU[0-9]';
SELECT 'Total sneakers: ' || COUNT(*) FROM products WHERE is_sneaker = true;
SQL

# Process P1 cutouts as part of the pipeline (small batch)
echo "=== Starting P1 cutout processing ==="
python3 scripts/process-cutouts.py --batch 200 --priority p1 --model isnet-general-use 2>&1

echo "=== Nightly Sync finished at $(date) ==="
