#!/usr/bin/env python3
"""
Check current geocoding status and progress
"""

import os
from sqlalchemy import create_engine, text
from urllib.parse import quote_plus

def check_geocoding_status():
    """Check current geocoding status"""
    try:
        # Database connection
        password = quote_plus('F@f@k0s!!')
        DATABASE_URL = f'postgresql://postgres:{password}@localhost:5432/bookbeach'
        engine = create_engine(DATABASE_URL)
        
        with engine.connect() as db:
            print("🌍 RESTAURANT GEOCODING STATUS CHECK")
            print("=" * 50)
            
            # Count total restaurants
            total_result = db.execute(text("SELECT COUNT(*) FROM restaurants")).fetchone()
            total_restaurants = total_result[0] if total_result else 0
            
            # Count restaurants with coordinates
            geocoded_result = db.execute(text("""
                SELECT COUNT(*) FROM restaurants 
                WHERE latitude IS NOT NULL AND longitude IS NOT NULL
            """)).fetchone()
            geocoded_count = geocoded_result[0] if geocoded_result else 0
            
            # Count restaurants still needing coordinates
            remaining_result = db.execute(text("""
                SELECT COUNT(*) FROM restaurants 
                WHERE latitude IS NULL OR longitude IS NULL
            """)).fetchone()
            remaining_count = remaining_result[0] if remaining_result else 0
            
            print(f"📊 Total restaurants: {total_restaurants}")
            print(f"✅ Restaurants with coordinates: {geocoded_count}")
            print(f"⏳ Restaurants still needing coordinates: {remaining_count}")
            print(f"📈 Completion: {(geocoded_count/total_restaurants*100):.1f}%")
            
            if remaining_count == 0:
                print("🎉 ALL RESTAURANTS HAVE COORDINATES!")
                
                # Show sample coordinates
                print("\n📍 Sample coordinates:")
                samples = db.execute(text("""
                    SELECT restaurant_name, latitude, longitude, city
                    FROM restaurants 
                    WHERE latitude IS NOT NULL AND longitude IS NOT NULL
                    ORDER BY restaurant_name
                    LIMIT 10
                """)).fetchall()
                
                for sample in samples:
                    print(f"   • {sample.restaurant_name}: {sample.latitude}, {sample.longitude} ({sample.city})")
                    
            else:
                print(f"🔄 Geocoding process still running... {remaining_count} restaurants remaining")
                
                # Show some examples of what's been completed
                print("\n📍 Recently geocoded restaurants:")
                recent = db.execute(text("""
                    SELECT restaurant_name, latitude, longitude, city
                    FROM restaurants 
                    WHERE latitude IS NOT NULL AND longitude IS NOT NULL
                    ORDER BY restaurant_name
                    LIMIT 5
                """)).fetchall()
                
                for restaurant in recent:
                    print(f"   • {restaurant.restaurant_name}: {restaurant.latitude}, {restaurant.longitude} ({restaurant.city})")
            
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    check_geocoding_status()