#!/usr/bin/env python3
"""
Run full restaurant photo download in batches
"""

import requests
import os
import uuid
import time
import re
from datetime import datetime
from urllib.parse import quote_plus
from sqlalchemy import create_engine, text
import random

def download_photos_for_all_restaurants():
    """Download photos for all restaurants in batches"""
    print("🚀 Starting full restaurant photo download...")
    
    # Database connection
    DATABASE_URL = f"postgresql://postgres:{quote_plus('F@f@k0s!!')}@localhost:5432/bookbeach"
    engine = create_engine(DATABASE_URL)
    
    # Photo directory
    photo_dir = "restaurant_photos"
    os.makedirs(photo_dir, exist_ok=True)
    
    session = requests.Session()
    session.headers.update({
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    })
    
    try:
        with engine.connect() as db:
            # Get all restaurants without photos
            restaurants = db.execute(text("""
                SELECT r.restaurant_id, r.restaurant_name, r.cuisine_type
                FROM restaurants r
                LEFT JOIN restaurant_photos rp ON r.restaurant_id = rp.restaurant_id
                WHERE rp.photo_id IS NULL
                ORDER BY r.restaurant_name
            """)).fetchall()
            
            print(f"📊 Found {len(restaurants)} restaurants without photos")
            
            if not restaurants:
                print("✅ All restaurants already have photos!")
                return
            
            successful = 0
            failed = 0
            
            # Photo source URLs
            photo_sources = [
                "https://picsum.photos/800/600?random={random_id}",
                "https://source.unsplash.com/800x600/?restaurant,food",
                "https://source.unsplash.com/800x600/?greek,taverna",
                "https://source.unsplash.com/800x600/?mediterranean,cuisine",
                "https://source.unsplash.com/800x600/?dining,interior"
            ]
            
            for i, (restaurant_id, restaurant_name, cuisine_type) in enumerate(restaurants, 1):
                print(f"\n📍 Progress: {i}/{len(restaurants)} - {restaurant_name}")
                
                try:
                    # Select photo source
                    photo_url = random.choice(photo_sources)
                    if "{random_id}" in photo_url:
                        photo_url = photo_url.format(random_id=random.randint(1, 10000))
                    
                    print(f"   📷 Downloading from: {photo_url}")
                    response = session.get(photo_url, timeout=15)
                    response.raise_for_status()
                    
                    # Create filename
                    safe_name = re.sub(r'[^\w\s-]', '', restaurant_name)
                    safe_name = re.sub(r'[-\s]+', '_', safe_name.strip())[:40]
                    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
                    filename = f"{safe_name}_{timestamp}_{i}.jpg"
                    filepath = os.path.join(photo_dir, filename)
                    
                    # Save photo
                    with open(filepath, 'wb') as f:
                        f.write(response.content)
                    
                    file_size = os.path.getsize(filepath)
                    print(f"   ✅ Downloaded: {filename} ({file_size} bytes)")
                    
                    # Save to database
                    photo_id = str(uuid.uuid4())
                    db.execute(text("""
                        INSERT INTO restaurant_photos (
                            photo_id, restaurant_id, photo_path, photo_url, photo_type,
                            file_size, is_primary, uploaded_at, created_at
                        ) VALUES (
                            :photo_id, :restaurant_id, :photo_path, :photo_url, :photo_type,
                            :file_size, :is_primary, NOW(), NOW()
                        )
                    """), {
                        'photo_id': photo_id,
                        'restaurant_id': str(restaurant_id),
                        'photo_path': filepath,
                        'photo_url': photo_url,
                        'photo_type': 'general',
                        'file_size': file_size,
                        'is_primary': True
                    })
                    
                    db.commit()
                    successful += 1
                    print(f"   💾 Saved to database")
                    
                except Exception as e:
                    failed += 1
                    print(f"   ❌ Error: {e}")
                
                # Progress checkpoint every 25 restaurants
                if i % 25 == 0:
                    print(f"\n📊 Checkpoint: {i}/{len(restaurants)} processed")
                    print(f"   ✅ Successful: {successful}")
                    print(f"   ❌ Failed: {failed}")
                
                # Be polite to image services
                time.sleep(0.5)
            
            # Final statistics
            print(f"\n🎉 Photo download completed!")
            print(f"   📊 Total processed: {len(restaurants)}")
            print(f"   ✅ Successful: {successful}")
            print(f"   ❌ Failed: {failed}")
            print(f"   📁 Photos stored in: {photo_dir}")
            
            # Verify final state
            total_photos = db.execute(text("SELECT COUNT(*) FROM restaurant_photos")).fetchone()[0]
            restaurants_with_photos = db.execute(text("""
                SELECT COUNT(DISTINCT restaurant_id) FROM restaurant_photos
            """)).fetchone()[0]
            
            print(f"\n📊 Final Database State:")
            print(f"   📷 Total photos: {total_photos}")
            print(f"   🏪 Restaurants with photos: {restaurants_with_photos}")
            
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    download_photos_for_all_restaurants()