#!/usr/bin/env python3
import requests
import json
import time
import os

# Get Apify API token from environment or keychain
APIFY_TOKEN = os.environ.get('APIFY_TOKEN', '')

if not APIFY_TOKEN:
    print("Getting Apify token...")
    # Try to get from openclaw config or prompt
    print("Please provide APIFY_TOKEN or set environment variable")
    exit(1)

ACTOR_ID = "compass/crawler-google-places"

# Define search regions
searches = {
    "hawkes_bay": [
        "packhouse Hastings New Zealand",
        "cool storage Hastings New Zealand", 
        "cold storage Napier New Zealand",
        "packhouse Hawke's Bay New Zealand"
    ],
    "bay_of_plenty": [
        "packhouse Tauranga New Zealand",
        "cool storage Tauranga New Zealand",
        "packhouse Te Puke New Zealand",
        "kiwifruit packhouse Bay of Plenty"
    ],
    "nelson": [
        "packhouse Nelson New Zealand",
        "cool storage Motueka New Zealand",
        "apple packhouse Nelson",
        "cold storage Nelson Tasman"
    ],
    "gisborne": [
        "packhouse Gisborne New Zealand",
        "cool storage Gisborne",
        "citrus packhouse Gisborne"
    ],
    "central_otago": [
        "packhouse Central Otago New Zealand",
        "cool storage Cromwell New Zealand",
        "packhouse Alexandra New Zealand"
    ]
}

all_results = []

for region, queries in searches.items():
    print(f"\n=== Scraping {region.replace('_', ' ').title()} ===")
    
    # Prepare input
    input_data = {
        "searchStringsArray": queries,
        "maxCrawledPlacesPerSearch": 50,
        "language": "en",
        "exportPlaceUrls": False,
        "includeWebResults": False
    }
    
    # Start actor run
    response = requests.post(
        f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs",
        params={"token": APIFY_TOKEN},
        json=input_data
    )
    
    if response.status_code != 201:
        print(f"Error starting run: {response.text}")
        continue
    
    run_data = response.json()
    run_id = run_data['data']['id']
    print(f"Started run {run_id}")
    
    # Wait for completion
    while True:
        status_response = requests.get(
            f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs/{run_id}",
            params={"token": APIFY_TOKEN}
        )
        
        status = status_response.json()['data']['status']
        print(f"Status: {status}")
        
        if status in ['SUCCEEDED', 'FAILED', 'ABORTED']:
            break
        
        time.sleep(10)
    
    if status != 'SUCCEEDED':
        print(f"Run failed with status: {status}")
        continue
    
    # Get results
    dataset_id = run_data['data']['defaultDatasetId']
    results_response = requests.get(
        f"https://api.apify.com/v2/datasets/{dataset_id}/items",
        params={"token": APIFY_TOKEN}
    )
    
    results = results_response.json()
    print(f"Found {len(results)} results")
    
    # Add region tag
    for item in results:
        item['apify_region'] = region
    
    all_results.extend(results)

# Save all results
with open('/Users/max/.openclaw/workspace/postharvest/nz-maps-raw.json', 'w') as f:
    json.dump(all_results, f, indent=2)

print(f"\n✅ Total results: {len(all_results)}")
print("Saved to nz-maps-raw.json")
