#!/usr/bin/env python3
"""
Image scraper for True Baby Cost products
Uses Google Images search to find and download product images
"""

import json
import os
import time
import urllib.request
import urllib.parse
import re
from datetime import datetime

# Configuration
DATA_DIR = '/workspace/stroller-app/data'
IMAGES_DIR = '/workspace/stroller-app/images'

CATEGORIES = [
    {'file': 'regional-strollers-research.json', 'dir': '', 'use_new_products': True},
    {'file': 'bottles.json', 'dir': 'bottles/', 'use_new_products': False},
    {'file': 'diapers.json', 'dir': 'diapers/', 'use_new_products': False},
    {'file': 'wipes.json', 'dir': 'wipes/', 'use_new_products': False},
    {'file': 'formula.json', 'dir': 'formula/', 'use_new_products': False},
    {'file': 'breast-pumps.json', 'dir': 'breast-pumps/', 'use_new_products': False}
]

def get_product_id(product, use_new_products):
    """Generate product ID from product data"""
    brand = product.get('brand', '')
    model = product.get('model', '')
    
    if use_new_products:
        # Strollers format - generate ID from brand and model
        product_id = f"{brand}-{model}".lower()
        product_id = re.sub(r'[^a-z0-9]+', '-', product_id)
        product_id = product_id.strip('-')
    else:
        # Other products - use existing ID
        product_id = product.get('id', '')
    
    return product_id, brand, model

def search_google_images(brand, model):
    """
    Search Google Images and extract first image URL
    Returns image URL or None
    """
    query = f"{brand} {model} product"
    search_url = f"https://www.google.com/search?q={urllib.parse.quote(query)}&tbm=isch"
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    }
    
    try:
        req = urllib.request.Request(search_url, headers=headers)
        with urllib.request.urlopen(req, timeout=15) as response:
            html = response.read().decode('utf-8', errors='ignore')
            
        # Find image URLs in the HTML
        # Google Images embeds image URLs in specific patterns
        patterns = [
            r'"ou":"(https?://[^"]+\.jpg[^"]*)"',
            r'"ou":"(https?://[^"]+\.jpeg[^"]*)"',
            r'"ou":"(https?://[^"]+\.png[^"]*)"',
            r'src="(https?://[^"]+\.jpg[^"]*)"',
            r'src="(https?://[^"]+\.jpeg[^"]*)"',
        ]
        
        for pattern in patterns:
            matches = re.findall(pattern, html)
            for url in matches:
                # Skip tiny Google thumbnails
                if 'gstatic.com' in url or 'encrypted-tbn' in url:
                    continue
                if len(url) > 50:  # Real image URLs are longer
                    return url
        
        return None
    except Exception as e:
        print(f"    Search error: {e}")
        return None

def download_image(url, output_path):
    """Download image from URL to output path"""
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    }
    
    try:
        req = urllib.request.Request(url, headers=headers)
        with urllib.request.urlopen(req, timeout=20) as response:
            data = response.read()
        
        # Ensure output directory exists
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        
        # Save image
        with open(output_path, 'wb') as f:
            f.write(data)
        
        return True
    except Exception as e:
        print(f"    Download error: {e}")
        return False

def process_product(product, category):
    """Process a single product - search and download image"""
    file_info = category
    use_new_products = file_info['use_new_products']
    
    product_id, brand, model = get_product_id(product, use_new_products)
    
    if not product_id or not brand or not model:
        return {'success': False, 'id': 'unknown', 'error': 'Missing product info'}
    
    output_dir = os.path.join(IMAGES_DIR, file_info['dir'])
    output_path = os.path.join(output_dir, f"{product_id}.jpg")
    
    # Skip if already exists
    if os.path.exists(output_path):
        print(f"  ✓ Already exists: {product_id}")
        return {'success': True, 'id': product_id}
    
    print(f"  Searching: {brand} {model}")
    
    # Search for image
    image_url = search_google_images(brand, model)
    
    if not image_url:
        print(f"  ✗ No image found: {product_id}")
        return {'success': False, 'id': product_id, 'error': 'No image found'}
    
    print(f"    Found: {image_url[:80]}...")
    
    # Download image
    if download_image(image_url, output_path):
        print(f"  ✓ Downloaded: {product_id}")
        return {'success': True, 'id': product_id}
    else:
        print(f"  ✗ Download failed: {product_id}")
        return {'success': False, 'id': product_id, 'error': 'Download failed'}

def process_category(category):
    """Process all products in a category"""
    file_path = os.path.join(DATA_DIR, category['file'])
    category_name = category['file'].replace('.json', '').replace('regional-strollers-research', 'strollers')
    
    print(f"\n📦 Processing {category_name}...")
    
    with open(file_path, 'r') as f:
        data = json.load(f)
    
    # Get products list
    if category['use_new_products']:
        products = data.get('new_products', [])
    else:
        products = data.get('products', [])
    
    # Create output directory
    output_dir = os.path.join(IMAGES_DIR, category['dir'])
    os.makedirs(output_dir, exist_ok=True)
    
    results = {'success': 0, 'failed': 0, 'failed_ids': []}
    
    for i, product in enumerate(products):
        print(f"[{i + 1}/{len(products)}]")
        
        result = process_product(product, category)
        
        if result['success']:
            results['success'] += 1
        else:
            results['failed'] += 1
            results['failed_ids'].append(result['id'])
        
        # Be polite - small delay between requests
        time.sleep(2)
    
    return results

def main():
    print("🚀 Starting image scraper...\n")
    
    all_results = {}
    
    for category in CATEGORIES:
        category_name = category['file'].replace('.json', '').replace('regional-strollers-research', 'strollers')
        all_results[category_name] = process_category(category)
    
    # Save results summary
    summary = {
        'scraped_at': datetime.utcnow().isoformat() + 'Z',
        'results': all_results
    }
    
    output_file = os.path.join(DATA_DIR, 'image-scrape-results.json')
    with open(output_file, 'w') as f:
        json.dump(summary, f, indent=2)
    
    print('\n✅ Scraping complete!')
    print('\nSummary:')
    
    total_success = 0
    total_failed = 0
    
    for category, results in all_results.items():
        success = results['success']
        failed = results['failed']
        total_success += success
        total_failed += failed
        print(f"  {category}: {success} success, {failed} failed")
    
    print(f"\nTotal: {total_success} success, {total_failed} failed")
    print(f"Results saved to: {output_file}")

if __name__ == '__main__':
    main()
