#!/bin/bash

###############################################################################
# Batch Contact Enrichment Script
# Processes facilities systematically using Hunter.io and web research
###############################################################################

HUNTER_API_KEY="fda8536970076bc3228c5b5fa6e19fdc407c43c9"
DATA_DIR="/Users/max/.openclaw/workspace/postharvest"
BATCH_FILE="$DATA_DIR/batch-1-data.json"
OUTPUT_FILE="$DATA_DIR/enriched-data.json"

echo "🚀 Batch Contact Enrichment Starting..."
echo ""

# Read batch data
if [ ! -f "$BATCH_FILE" ]; then
    echo "❌ Error: $BATCH_FILE not found"
    exit 1
fi

# Function to extract domain from URL
extract_domain() {
    echo "$1" | sed -E 's|https?://([^/]+).*|\1|' | sed 's/^www\.//'
}

# Function to search domain with Hunter.io
search_domain() {
    local domain=$1
    local company=$2
    
    echo "  🔍 Searching $domain with Hunter.io..."
    
    curl -s "https://api.hunter.io/v2/domain-search?domain=$domain&api_key=$HUNTER_API_KEY&limit=10" \
        | jq -r '.data.emails[]? | "\(.first_name // "N/A")|\(.last_name // "N/A")|\(.position // "N/A")|\(.value)|\(.confidence)|\(.linkedin // "N/A")"' \
        | while IFS='|' read -r first last position email confidence linkedin; do
            echo "    ✓ $first $last - $position - $email (confidence: $confidence%)"
        done
}

# Process first 10 facilities
counter=1
total=$(jq 'length' "$BATCH_FILE")

echo "📊 Processing $total facilities from batch..."
echo ""

jq -c '.[]' "$BATCH_FILE" | while read -r facility; do
    company=$(echo "$facility" | jq -r '.company')
    website=$(echo "$facility" | jq -r '.website // empty')
    region=$(echo "$facility" | jq -r '.region')
    
    echo "[$counter/$total] $company ($region)"
    
    if [ -n "$website" ] && [ "$website" != "null" ]; then
        domain=$(extract_domain "$website")
        echo "  🌐 Website: $website"
        echo "  🔗 Domain: $domain"
        
        # Search with Hunter.io
        search_domain "$domain" "$company"
        
        # Rate limiting - Hunter.io free tier
        echo "  ⏳ Waiting 2s (rate limit)..."
        sleep 2
    else
        echo "  ⚠️  No website available"
    fi
    
    echo ""
    ((counter++))
done

echo "✅ Batch processing complete!"
echo ""
echo "📋 Next steps:"
echo "   1. Review Hunter.io results above"
echo "   2. Use LinkedIn searches for missing contacts"
echo "   3. Update enriched-data.json with findings"
echo "   4. Run next batch"
