#!/usr/bin/env python3
"""
Add address columns to restaurants table
"""

import os
from sqlalchemy import create_engine, text
from urllib.parse import quote_plus

def add_restaurant_address_columns():
    """Add address, city, and country_id columns to restaurants table"""
    try:
        # Database connection
        password = quote_plus('F@f@k0s!!')
        DATABASE_URL = f'postgresql://postgres:{password}@localhost:5432/bookbeach'
        engine = create_engine(DATABASE_URL)
        
        # Read the SQL migration script
        with open('add_restaurant_address_columns.sql', 'r') as f:
            sql_script = f.read()
        
        with engine.connect() as db:
            print("🔄 Adding address columns to restaurants table...")
            
            # Execute the migration
            db.execute(text(sql_script))
            db.commit()
            
            print("✅ Address columns added successfully!")
            
            # Verify the changes
            result = db.execute(text("""
                SELECT column_name, data_type, is_nullable 
                FROM information_schema.columns 
                WHERE table_name = 'restaurants' 
                AND column_name IN ('address', 'city', 'country_id')
                ORDER BY column_name
            """)).fetchall()
            
            print("\n📋 New columns added:")
            for row in result:
                nullable = 'NULL' if row.is_nullable == 'YES' else 'NOT NULL'
                print(f"   {row.column_name}: {row.data_type} {nullable}")
            
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    add_restaurant_address_columns()