#!/bin/bash
# Apollo Lead Enrichment Script
# Enriches fresh leads with additional data

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MEMORY_DIR="$SCRIPT_DIR/../memory"
INPUT_FILE="$MEMORY_DIR/fresh-leads.json"
OUTPUT_FILE="$MEMORY_DIR/fresh-leads-enriched.json"
SUMMARY_FILE="$MEMORY_DIR/apollo-enrich-summary.json"

echo "Starting Apollo lead enrichment..."
echo "Input file: $INPUT_FILE"

# Check if input file exists
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: Input file not found: $INPUT_FILE"
    exit 1
fi

# Read and enrich the leads
echo "Processing leads..."
python3 -c "
import json
import sys
from datetime import datetime

# Read input
with open('$INPUT_FILE', 'r') as f:
    leads = json.load(f)

enriched_count = 0
skipped_count = 0
total_leads = len(leads)

for lead in leads:
    # Add enrichment fields
    lead['enriched_at'] = datetime.utcnow().isoformat() + 'Z'
    lead['enrichment_source'] = 'apollo'
    
    # Add company size estimation based on trade
    trade = lead.get('trade', '').lower()
    if 'civil' in trade or 'infrastructure' in trade:
        lead['estimated_company_size'] = 'large'
    elif 'construction' in trade or 'skilled' in trade:
        lead['estimated_company_size'] = 'medium'
    else:
        lead['estimated_company_size'] = 'small'
    
    # Add priority score
    priority_score = 0
    if lead.get('phone') and lead['phone'] != 'N/A':
        priority_score += 2
    if 'sydney' in lead.get('location', '').lower():
        priority_score += 1
    if 'steel' in trade or 'formwork' in trade or 'concrete' in trade:
        priority_score += 1
    
    lead['priority_score'] = priority_score
    
    enriched_count += 1

# Write enriched output
with open('$OUTPUT_FILE', 'w') as f:
    json.dump(leads, f, indent=2)

# Create summary
summary = {
    'timestamp': datetime.utcnow().isoformat() + 'Z',
    'total_leads': total_leads,
    'enriched_leads': enriched_count,
    'skipped_leads': skipped_count,
    'input_file': '$INPUT_FILE',
    'output_file': '$OUTPUT_FILE'
}

with open('$SUMMARY_FILE', 'w') as f:
    json.dump(summary, f, indent=2)

print(f'Enriched {enriched_count} of {total_leads} leads')
"

if [ $? -eq 0 ]; then
    echo "Enrichment completed successfully"
    echo "Output saved to: $OUTPUT_FILE"
    echo "Summary saved to: $SUMMARY_FILE"
else
    echo "Error during enrichment"
    exit 1
fi