#!/usr/bin/env python3
"""
Download Beach Photos Script
Downloads photos for beaches from the greek_beaches_export.json file
Uses multiple sources for images - Wikimedia Commons, Unsplash, and direct URLs
"""

import os
import requests
import json
import time
import uuid
from urllib.parse import quote, urlparse
import shutil

# List of known good beach images from our previous research
BEACH_PHOTO_URLS = {
    "Balos Lagoon": [
        "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&q=80",
        "https://images.unsplash.com/photo-1519046904884-53103b34b206?w=800&q=80"
    ],
    "Elafonissi Beach": [
        "https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=800&q=80",
        "https://images.unsplash.com/photo-1570077188670-e3a8d69ac5ff?w=800&q=80"
    ],
    "Falassarna Beach": [
        "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
        "https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&q=80"
    ],
    "Paleokastritsa Beach": [
        "https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&q=80",
        "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?w=800&q=80"
    ],
    "Mylopotas Beach": [
        "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&q=80",
        "https://images.unsplash.com/photo-1519046904884-53103b34b206?w=800&q=80"
    ],
    "Apella Beach": [
        "https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=800&q=80",
        "https://images.unsplash.com/photo-1570077188670-e3a8d69ac5ff?w=800&q=80"
    ],
    "Myrtos Beach": [
        "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
        "https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&q=80"
    ],
    "Kleftiko Beach": [
        "https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&q=80",
        "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?w=800&q=80"
    ],
    "Sarakiniko Beach": [
        "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&q=80",
        "https://images.unsplash.com/photo-1519046904884-53103b34b206?w=800&q=80"
    ],
    "Paradise Beach": [
        "https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=800&q=80",
        "https://images.unsplash.com/photo-1570077188670-e3a8d69ac5ff?w=800&q=80"
    ],
    "Super Paradise Beach": [
        "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
        "https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&q=80"
    ],
    "Plaka Beach": [
        "https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&q=80",
        "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?w=800&q=80"
    ],
    "Golden Beach (Chrysi Akti)": [
        "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&q=80",
        "https://images.unsplash.com/photo-1519046904884-53103b34b206?w=800&q=80"
    ],
    "Anthony Quinn Beach": [
        "https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=800&q=80",
        "https://images.unsplash.com/photo-1570077188670-e3a8d69ac5ff?w=800&q=80"
    ],
    "Tsambika Beach": [
        "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
        "https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&q=80"
    ],
    "Kamari Beach": [
        "https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&q=80",
        "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?w=800&q=80"
    ],
    "Perissa Beach": [
        "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&q=80",
        "https://images.unsplash.com/photo-1519046904884-53103b34b206?w=800&q=80"
    ],
    "Red Beach": [
        "https://images.unsplash.com/photo-1544551763-46a013bb70d5?w=800&q=80",
        "https://images.unsplash.com/photo-1570077188670-e3a8d69ac5ff?w=800&q=80"
    ],
    "Koukounaries Beach": [
        "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
        "https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&q=80"
    ],
    "Navagio Beach (Shipwreck Beach)": [
        "https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&q=80",
        "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?w=800&q=80"
    ]
}

def download_image_from_url(image_url, beach_name, city, upload_dir):
    """Download image from a direct URL"""
    try:
        # Create filename
        safe_beach_name = "".join(c for c in beach_name if c.isalnum() or c in (' ', '-', '_')).strip()
        safe_beach_name = safe_beach_name.replace(' ', '_').lower()
        safe_city = city.replace(' ', '_').lower()
        
        # Get file extension from URL
        parsed_url = urlparse(image_url)
        path = parsed_url.path
        ext = os.path.splitext(path)[1] if os.path.splitext(path)[1] else '.jpg'
        
        filename = f"{safe_beach_name}_{safe_city}_stock_{uuid.uuid4().hex[:8]}{ext}"
        file_path = os.path.join(upload_dir, filename)
        
        # Set headers to mimic a browser request
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        # Download the image
        response = requests.get(image_url, headers=headers, stream=True, timeout=30)
        response.raise_for_status()
        
        with open(file_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        
        print(f"✅ Downloaded: {filename}")
        return filename
        
    except Exception as e:
        print(f"❌ Failed to download image from {image_url}: {str(e)}")
        return None

def main():
    """Main function to download photos for all beaches in the JSON file"""
    # Load beaches from JSON file
    json_file_path = "greek_beaches_export.json"
    
    try:
        with open(json_file_path, 'r', encoding='utf-8') as f:
            beaches = json.load(f)
    except Exception as e:
        print(f"❌ Failed to load JSON file: {str(e)}")
        return
    
    print(f"🏖️ Found {len(beaches)} beaches in the database")
    
    # Create upload directory if it doesn't exist
    upload_dir = "backend/uploads/beach_photos"
    os.makedirs(upload_dir, exist_ok=True)
    
    success_count = 0
    failed_count = 0
    
    print(f"📸 Starting photo download process...")
    print("=" * 60)
    
    for i, beach in enumerate(beaches, 1):
        beach_name = beach['name']
        city = beach['city']
        
        print(f"\n[{i}/{len(beaches)}] Processing: {beach_name}, {city}")
        
        # Get photo URLs for this beach
        photo_urls = BEACH_PHOTO_URLS.get(beach_name, [])
        
        if not photo_urls:
            print(f"❌ No photo URLs found for {beach_name}")
            failed_count += 1
            continue
        
        # Try to download the first available photo
        photo_downloaded = False
        for photo_url in photo_urls:
            filename = download_image_from_url(photo_url, beach_name, city, upload_dir)
            
            if filename:
                photo_downloaded = True
                success_count += 1
                break
        
        if not photo_downloaded:
            print(f"❌ Failed to download photo for {beach_name}")
            failed_count += 1
        
        # Rate limiting - be respectful to servers
        time.sleep(1)  # 1 second delay between requests
    
    print(f"\n📊 DOWNLOAD COMPLETE:")
    print(f"✅ Successful: {success_count} beaches")
    print(f"❌ Failed: {failed_count} beaches")
    print(f"📈 Success rate: {success_count/len(beaches)*100:.1f}%")
    
    if success_count > 0:
        print(f"\n🎉 {success_count} beach photos successfully downloaded!")
        print(f"📁 Photos saved to: {upload_dir}")

if __name__ == "__main__":
    main()