#!/bin/bash
# Run Apify Google Maps scraper for 4 US states
# Estimated: 1,300 results, ~$1-2 from $5 free credit

APIFY_TOKEN="apify_api_eH9m0p4HbvQo1D4k7OjMxs6TKFYuEf0sUWRo"
ACTOR_ID="nwua9Gu5YrADL7ZDj"  # Google Maps Scraper

echo "🚀 Starting 4-state apple facility scrape..."
echo "States: NY, PA, MI, WA"
echo "Estimated results: 1,300 facilities"
echo ""

# Function to run a single search
run_search() {
    local query="$1"
    local max_results="$2"
    local state="$3"
    local type="$4"
    
    echo "📍 Searching: $query (max $max_results results)"
    
    # Create input JSON
    cat > /tmp/apify-input.json << EOF
{
  "searchStringsArray": ["$query"],
  "maxCrawledPlacesPerSearch": $max_results,
  "language": "en",
  "maxReviews": 0,
  "maxImages": 0,
  "exportPlaceUrls": false,
  "includeWebResults": false
}
EOF
    
    # Run Apify actor
    curl -s "https://api.apify.com/v2/acts/$ACTOR_ID/runs" \
        -H "Authorization: Bearer $APIFY_TOKEN" \
        -H "Content-Type: application/json" \
        -d @/tmp/apify-input.json \
        > /tmp/apify-run.json
    
    # Extract run ID
    RUN_ID=$(cat /tmp/apify-run.json | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
    
    if [ -z "$RUN_ID" ]; then
        echo "❌ Failed to start run for: $query"
        return 1
    fi
    
    echo "   Run ID: $RUN_ID"
    echo "   Waiting for completion..."
    
    # Wait for completion (poll every 10 seconds)
    while true; do
        STATUS=$(curl -s "https://api.apify.com/v2/acts/$ACTOR_ID/runs/$RUN_ID" \
            -H "Authorization: Bearer $APIFY_TOKEN" \
            | grep -o '"status":"[^"]*"' | head -1 | cut -d'"' -f4)
        
        if [ "$STATUS" = "SUCCEEDED" ]; then
            echo "   ✅ Complete!"
            break
        elif [ "$STATUS" = "FAILED" ] || [ "$STATUS" = "ABORTED" ]; then
            echo "   ❌ Failed with status: $STATUS"
            return 1
        fi
        
        echo "   ⏳ Status: $STATUS ..."
        sleep 10
    done
    
    # Download results
    OUTPUT_FILE="/Users/max/.openclaw/workspace/postharvest/apify-${state}-${type}.json"
    curl -s "https://api.apify.com/v2/acts/$ACTOR_ID/runs/$RUN_ID/dataset/items" \
        -H "Authorization: Bearer $APIFY_TOKEN" \
        > "$OUTPUT_FILE"
    
    COUNT=$(cat "$OUTPUT_FILE" | grep -o '"title"' | wc -l)
    echo "   💾 Saved $COUNT results to: apify-${state}-${type}.json"
    echo ""
    
    sleep 5  # Rate limit courtesy
}

# Run all searches
run_search "apple orchard New York, USA" 200 "NY" "orchard"
run_search "apple grower New York, USA" 200 "NY" "grower"
run_search "cold storage apples New York, USA" 100 "NY" "storage"
run_search "apple orchard Pennsylvania, USA" 150 "PA" "orchard"
run_search "apple grower Pennsylvania, USA" 150 "PA" "grower"
run_search "apple orchard Michigan, USA" 150 "MI" "orchard"
run_search "apple grower Michigan, USA" 150 "MI" "grower"
run_search "apple packer Washington State, USA" 100 "WA" "packer"
run_search "apple cold storage Washington State, USA" 100 "WA" "storage"

echo "🎉 All searches complete!"
echo ""
echo "Next step: Merge and deduplicate results"
