#!/bin/bash
# Seed WooCommerce with sample products including images

download_and_attach() {
  local url="$1"
  local title="$2"
  local post_id="$3"
  local filename=$(echo "$title" | tr ' ' '-' | tr '[:upper:]' '[:lower:]').jpg

  curl -sL "$url" -o "/tmp/$filename"
  local attach_id=$(wp media import "/tmp/$filename" --title="$title" --porcelain --allow-root 2>/dev/null)
  if [ -n "$attach_id" ]; then
    wp post meta update "$post_id" _thumbnail_id "$attach_id" --allow-root 2>/dev/null
    echo "  Image attached (ID: $attach_id)"
  fi
  rm -f "/tmp/$filename"
}

create_product() {
  local name="$1"
  local price="$2"
  local sale="$3"
  local cat_id="$4"
  local desc="$5"
  local short="$6"
  local img_url="$7"
  local sku="$8"

  local args="--name=\"$name\" --regular_price=\"$price\" --categories='[{\"id\":$cat_id}]' --description=\"$desc\" --short_description=\"$short\" --sku=\"$sku\" --manage_stock=true --stock_quantity=$((RANDOM % 50 + 10)) --allow-root --user=admin --porcelain"

  if [ -n "$sale" ]; then
    args="$args --sale_price=\"$sale\""
  fi

  local pid=$(eval wp wc product create $args 2>/dev/null)
  echo "Created: $name (ID: $pid)"

  if [ -n "$img_url" ] && [ -n "$pid" ]; then
    download_and_attach "$img_url" "$name" "$pid"
  fi
}

echo "=== Seeding Products ==="

# Electronics
create_product \
  "Wireless Bluetooth Headphones" "79.99" "59.99" 16 \
  "Experience premium sound quality with these over-ear wireless Bluetooth headphones. Featuring active noise cancellation, 30-hour battery life, and ultra-soft memory foam ear cushions. Compatible with all Bluetooth-enabled devices. Built-in microphone for hands-free calls." \
  "Premium wireless headphones with active noise cancellation and 30-hour battery life." \
  "https://picsum.photos/seed/headphones/800/800" \
  "ELEC-001"

create_product \
  "Smart Watch Pro" "199.99" "179.99" 16 \
  "Stay connected and track your fitness with the Smart Watch Pro. Features include heart rate monitoring, GPS tracking, sleep analysis, and 50+ workout modes. Water resistant to 50 meters with a stunning AMOLED display. Syncs seamlessly with iOS and Android." \
  "Advanced smartwatch with heart rate monitor, GPS, and 50+ workout modes." \
  "https://picsum.photos/seed/smartwatch/800/800" \
  "ELEC-002"

create_product \
  "Portable Bluetooth Speaker" "49.99" "" 16 \
  "Take your music anywhere with this compact yet powerful Bluetooth speaker. Delivers 360-degree sound with deep bass. IPX7 waterproof rating makes it perfect for pool parties and outdoor adventures. Up to 12 hours of playtime on a single charge." \
  "Compact waterproof Bluetooth speaker with 360-degree sound and 12-hour battery." \
  "https://picsum.photos/seed/speaker/800/800" \
  "ELEC-003"

create_product \
  "4K Webcam with Ring Light" "89.99" "69.99" 16 \
  "Upgrade your video calls with this professional 4K webcam. Built-in adjustable ring light with 3 brightness levels ensures you always look your best. Auto-focus, noise-reducing dual microphones, and plug-and-play USB-C connectivity." \
  "Professional 4K webcam with built-in ring light and dual microphones." \
  "https://picsum.photos/seed/webcam/800/800" \
  "ELEC-004"

# Clothing
create_product \
  "Classic Fit Cotton T-Shirt" "24.99" "19.99" 17 \
  "A wardrobe essential made from 100% organic cotton. This classic fit t-shirt features a reinforced crew neck, double-stitched hems, and pre-shrunk fabric. Available in multiple colors. Machine washable and built to last through countless wears." \
  "100% organic cotton classic fit t-shirt. Comfortable, durable, and versatile." \
  "https://picsum.photos/seed/tshirt/800/800" \
  "CLOTH-001"

create_product \
  "Slim Fit Denim Jeans" "59.99" "" 17 \
  "Crafted from premium stretch denim with a modern slim fit silhouette. Features a mid-rise waist, five-pocket styling, and a zip fly with button closure. The perfect blend of comfort and style for everyday wear. 98% cotton, 2% elastane." \
  "Premium stretch denim jeans with a modern slim fit and classic five-pocket design." \
  "https://picsum.photos/seed/jeans/800/800" \
  "CLOTH-002"

create_product \
  "Merino Wool Zip-Up Hoodie" "89.99" "74.99" 17 \
  "Luxuriously soft merino wool hoodie perfect for layering. Temperature regulating, moisture-wicking, and naturally odor-resistant. Features a full-zip front, kangaroo pockets, and ribbed cuffs. Ideal for travel, outdoor activities, or casual everyday wear." \
  "Premium merino wool hoodie. Temperature regulating and naturally odor-resistant." \
  "https://picsum.photos/seed/hoodie/800/800" \
  "CLOTH-003"

create_product \
  "Waterproof Winter Jacket" "149.99" "119.99" 17 \
  "Brave the elements in style with this fully waterproof and windproof winter jacket. Features sealed seams, a detachable insulated hood, multiple interior and exterior pockets, and adjustable cuffs. Rated for temperatures down to -20°C." \
  "Fully waterproof and windproof winter jacket rated to -20°C." \
  "https://picsum.photos/seed/jacket/800/800" \
  "CLOTH-004"

# Home & Kitchen
create_product \
  "Stainless Steel French Press" "34.99" "" 18 \
  "Brew the perfect cup of coffee every morning with this double-walled stainless steel French press. The vacuum insulation keeps coffee hot for hours. 4-level filtration system eliminates grounds for a smooth, rich cup. Holds 34 oz (about 8 cups)." \
  "Double-walled stainless steel French press with 4-level filtration. 34 oz capacity." \
  "https://picsum.photos/seed/frenchpress/800/800" \
  "HOME-001"

create_product \
  "Bamboo Cutting Board Set" "29.99" "24.99" 18 \
  "Set of 3 premium bamboo cutting boards in small, medium, and large sizes. Made from sustainably sourced Moso bamboo that is 16% harder than maple. Features deep juice grooves and easy-grip handles. Knife-friendly surface that won't dull your blades." \
  "Set of 3 sustainable bamboo cutting boards with juice grooves. Knife-friendly." \
  "https://picsum.photos/seed/cuttingboard/800/800" \
  "HOME-002"

create_product \
  "Ceramic Non-Stick Cookware Set" "129.99" "99.99" 18 \
  "Complete 10-piece ceramic non-stick cookware set. Includes 8\" and 10\" frying pans, 1.5qt and 2.5qt saucepans with lids, 5qt dutch oven with lid, and a steamer insert. PFOA-free ceramic coating. Oven safe to 450°F. Works on all cooktops including induction." \
  "10-piece PFOA-free ceramic cookware set. Oven safe to 450°F, induction compatible." \
  "https://picsum.photos/seed/cookware/800/800" \
  "HOME-003"

create_product \
  "Scented Soy Candle Collection" "39.99" "" 18 \
  "Set of 4 hand-poured soy wax candles in artisan glass jars. Scents include Lavender Fields, Vanilla Bean, Ocean Breeze, and Cedarwood Sage. Each candle provides approximately 45 hours of clean, even burn. Made with natural cotton wicks and premium fragrance oils." \
  "Set of 4 hand-poured soy candles. 45-hour burn time each. Natural ingredients." \
  "https://picsum.photos/seed/candles/800/800" \
  "HOME-004"

# Sports & Outdoors
create_product \
  "Yoga Mat with Alignment Lines" "39.99" "34.99" 19 \
  "Premium 6mm thick yoga mat with laser-etched alignment lines to help perfect your form. Made from eco-friendly TPE material that provides excellent grip even when wet. Lightweight at just 2.5 lbs with an included carrying strap. 72\" x 26\" dimensions." \
  "Eco-friendly 6mm yoga mat with alignment guides. Non-slip, lightweight with carrying strap." \
  "https://picsum.photos/seed/yogamat/800/800" \
  "SPORT-001"

create_product \
  "Insulated Water Bottle 32oz" "27.99" "" 19 \
  "Triple-insulated stainless steel water bottle keeps drinks cold for 24 hours or hot for 12. Features a leak-proof flex cap, wide mouth for easy filling and cleaning, and a powder-coated exterior that resists condensation. BPA-free and dishwasher safe." \
  "Triple-insulated 32oz bottle. Cold 24hrs, hot 12hrs. Leak-proof and BPA-free." \
  "https://picsum.photos/seed/waterbottle/800/800" \
  "SPORT-002"

create_product \
  "Resistance Band Set" "22.99" "18.99" 19 \
  "Complete set of 5 resistance bands ranging from 5 to 50 lbs. Made from natural latex with reinforced stitching for durability. Includes door anchor, ankle straps, and cushioned handles. Perfect for home workouts, physical therapy, and travel fitness." \
  "5-piece resistance band set (5-50 lbs) with door anchor and handles." \
  "https://picsum.photos/seed/resistancebands/800/800" \
  "SPORT-003"

create_product \
  "Camping Hammock with Mosquito Net" "54.99" "44.99" 19 \
  "Ultra-lightweight parachute nylon hammock with integrated mosquito net. Supports up to 440 lbs. Comes with tree-friendly straps, aluminum carabiners, and a compact stuff sack. Sets up in under 2 minutes. Perfect for camping, hiking, and backyard relaxation." \
  "Lightweight camping hammock with mosquito net. 440 lb capacity, 2-minute setup." \
  "https://picsum.photos/seed/hammock/800/800" \
  "SPORT-004"

# Books
create_product \
  "The Art of Simple Living" "16.99" "12.99" 20 \
  "A beautifully written guide to finding joy in simplicity. Drawing from minimalist philosophy and practical wisdom, this book offers 100 actionable tips for decluttering your home, mind, and schedule. Includes journaling prompts and reflection exercises. Hardcover, 256 pages." \
  "A practical guide to minimalist living with 100 actionable tips. Hardcover, 256 pages." \
  "https://picsum.photos/seed/book1/800/800" \
  "BOOK-001"

create_product \
  "Mastering Python Programming" "44.99" "39.99" 20 \
  "The comprehensive guide to Python programming for beginners and intermediate developers. Covers fundamentals through advanced topics including data structures, algorithms, web development with Django, data science with Pandas, and machine learning basics. 600+ pages with hands-on projects." \
  "Comprehensive Python guide from basics to advanced. 600+ pages with hands-on projects." \
  "https://picsum.photos/seed/pythonbook/800/800" \
  "BOOK-002"

create_product \
  "World Atlas of Coffee" "29.99" "" 20 \
  "An indispensable guide for coffee lovers. Explores coffee-growing regions across the globe, from Ethiopian highlands to Colombian valleys. Covers brewing methods, tasting notes, and the science behind the perfect cup. Includes stunning photography and detailed maps. Hardcover, 352 pages." \
  "Explore global coffee culture with brewing guides, tasting notes, and photography. 352 pages." \
  "https://picsum.photos/seed/coffeebook/800/800" \
  "BOOK-003"

create_product \
  "Beginner's Guide to Watercolor" "21.99" "17.99" 20 \
  "Unlock your creativity with this step-by-step watercolor painting guide. Covers essential techniques including wet-on-wet, glazing, and dry brush. Features 20 guided projects from simple florals to stunning landscapes. Includes a color mixing chart and materials guide. Paperback, 192 pages." \
  "Step-by-step watercolor guide with 20 projects. Perfect for beginners. 192 pages." \
  "https://picsum.photos/seed/watercolor/800/800" \
  "BOOK-004"

echo ""
echo "=== Done! All products seeded ==="
