#!/bin/bash
# Monitor natural spots seeding progress

echo "🌲 Natural Spots Seeding Monitor"
echo "================================"
echo ""

# Check if process is running
if pgrep -f "seed-natural-spots.mjs" > /dev/null; then
    echo "✅ Seeding process is running"
    echo ""
    
    # Show last 20 lines of output (if redirected to file)
    if [ -f "scripts/seed-natural-spots.log" ]; then
        echo "📊 Recent activity:"
        tail -n 20 scripts/seed-natural-spots.log
    else
        echo "💡 Tip: Run with output redirection to monitor:"
        echo "   node scripts/seed-natural-spots.mjs > scripts/seed-natural-spots.log 2>&1 &"
    fi
    
    echo ""
    echo "⏱️  Runtime:"
    ps -p $(pgrep -f "seed-natural-spots.mjs") -o etime= | xargs echo
    
else
    echo "❌ Seeding process not running"
    echo ""
    
    # Check if it finished successfully
    if [ -f "scripts/seed-natural-spots.log" ]; then
        if grep -q "NATURAL SPOTS SEEDING COMPLETE" scripts/seed-natural-spots.log; then
            echo "✅ Seeding completed successfully!"
            echo ""
            echo "📊 Final stats:"
            grep -A 10 "Statistics:" scripts/seed-natural-spots.log | tail -n 10
        else
            echo "⚠️  Process may have crashed. Last 30 lines:"
            tail -n 30 scripts/seed-natural-spots.log
        fi
    fi
fi

echo ""
echo "📁 Check failed tasks:"
if [ -f "scripts/seed-natural-spots-failed.json" ]; then
    FAILED_COUNT=$(grep -o '"region"' scripts/seed-natural-spots-failed.json | wc -l)
    echo "   ⚠️  $FAILED_COUNT regions failed"
    echo "   Retry with: node scripts/seed-natural-spots-retry.mjs"
else
    echo "   ✅ No failed tasks"
fi

echo ""
echo "🔄 POI Database:"
echo "   Check Firestore console: poi_database collection"
