#!/usr/bin/env python3
"""Run all 9 Apify Google Maps searches and download results."""
import requests
import json
import time
import sys
from pathlib import Path

APIFY_TOKEN = "apify_api_s5UWN0W1FkB0cmjtawlkMLIFof2vfu3faGOR"
ACTOR_ID = "nwua9Gu5YrADL7ZDj"
OUTPUT_DIR = Path("/Users/max/.openclaw/workspace/postharvest/apify-results")
OUTPUT_DIR.mkdir(exist_ok=True)

# Skip first search (already running: xr9RcREXaVCOKRz4B)
searches = [
    # {"query": "apple orchard", "location": "New York, USA", "max": 200, "state": "NY", "type": "orchard"},  # SKIP - already running
    {"query": "apple grower", "location": "New York, USA", "max": 200, "state": "NY", "type": "grower"},
    {"query": "cold storage apples", "location": "New York, USA", "max": 100, "state": "NY", "type": "storage"},
    {"query": "apple orchard", "location": "Pennsylvania, USA", "max": 150, "state": "PA", "type": "orchard"},
    {"query": "apple grower", "location": "Pennsylvania, USA", "max": 150, "state": "PA", "type": "grower"},
    {"query": "apple orchard", "location": "Michigan, USA", "max": 150, "state": "MI", "type": "orchard"},
    {"query": "apple grower", "location": "Michigan, USA", "max": 150, "state": "MI", "type": "grower"},
    {"query": "apple packer", "location": "Washington State, USA", "max": 100, "state": "WA", "type": "packer"},
    {"query": "apple cold storage", "location": "Washington State, USA", "max": 100, "state": "WA", "type": "storage"},
]

def start_run(search):
    """Start an Apify run and return run ID."""
    input_data = {
        "searchStringsArray": [search["query"]],
        "locationQuery": search["location"],
        "maxCrawledPlacesPerSearch": search["max"],
        "language": "en",
        "maxReviews": 0,
        "maxImages": 0,
        "exportPlaceUrls": False,
        "includeWebResults": False
    }
    
    response = requests.post(
        f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs",
        headers={"Authorization": f"Bearer {APIFY_TOKEN}"},
        json=input_data
    )
    response.raise_for_status()
    return response.json()["data"]["id"]

def wait_for_run(run_id, search):
    """Wait for run to complete."""
    print(f"  Waiting for completion...", flush=True)
    while True:
        response = requests.get(
            f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs/{run_id}",
            headers={"Authorization": f"Bearer {APIFY_TOKEN}"}
        )
        data = response.json()["data"]
        status = data["status"]
        
        if status == "SUCCEEDED":
            results = data.get("stats", {}).get("outputResults", 0)
            print(f"  ✅ Complete! {results} results")
            return True
        elif status in ["FAILED", "ABORTED"]:
            print(f"  ❌ Failed with status: {status}")
            return False
        
        print(f"  ⏳ Status: {status}...", flush=True)
        time.sleep(10)

def download_results(run_id, state, type_):
    """Download results to JSON file."""
    response = requests.get(
        f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs/{run_id}/dataset/items",
        headers={"Authorization": f"Bearer {APIFY_TOKEN}"},
        params={"format": "json"}
    )
    
    output_file = OUTPUT_DIR / f"{state}-{type_}.json"
    output_file.write_text(response.text)
    
    data = response.json()
    print(f"  💾 Saved {len(data)} results to: {output_file.name}")
    return len(data)

# Process all searches
total_results = 0
for i, search in enumerate(searches, 1):
    print(f"\n📍 [{i}/8] {search['query']} in {search['location']} (max {search['max']})")
    
    try:
        run_id = start_run(search)
        print(f"  Run ID: {run_id}")
        
        if wait_for_run(run_id, search):
            count = download_results(run_id, search["state"], search["type"])
            total_results += count
        
        time.sleep(2)  # Rate limit
        
    except Exception as e:
        print(f"  ❌ Error: {e}")
        continue

# Also download the first run (NY orchards) when it finishes
print(f"\n📍 [Checking first run] apple orchard in New York, USA")
print("  Run ID: xr9RcREXaVCOKRz4B (started earlier)")
if wait_for_run("xr9RcREXaVCOKRz4B", {"query": "apple orchard"}):
    count = download_results("xr9RcREXaVCOKRz4B", "NY", "orchard")
    total_results += count

print(f"\n🎉 All searches complete!")
print(f"📊 Total results: {total_results}")
print(f"📁 Files saved to: {OUTPUT_DIR}")
