#!/usr/bin/env python3
"""
Clean up phantom restaurant_uuid constraint
"""

from sqlalchemy import create_engine, text
from urllib.parse import quote_plus

def cleanup_phantom_constraint():
    DATABASE_URL = f"postgresql://postgres:{quote_plus('F@f@k0s!!')}@localhost:5432/bookbeach"
    engine = create_engine(DATABASE_URL)
    
    with engine.connect() as db:
        print("🔍 Checking for phantom constraints...")
        
        # Check if the constraint still exists
        constraint_exists = db.execute(text("""
            SELECT 1 FROM pg_constraint 
            WHERE conname = 'restaurants_restaurant_uuid_key'
              AND conrelid = 'restaurants'::regclass
        """)).fetchone()
        
        if constraint_exists:
            print("Found phantom constraint: restaurants_restaurant_uuid_key")
            try:
                db.execute(text("""
                    ALTER TABLE restaurants DROP CONSTRAINT restaurants_restaurant_uuid_key
                """))
                print("✅ Dropped phantom constraint: restaurants_restaurant_uuid_key")
                db.commit()
            except Exception as e:
                print(f"❌ Error dropping constraint: {e}")
                db.rollback()
        else:
            print("✅ No phantom constraint found")
        
        # Final verification
        print(f"\n📋 Final restaurants table constraints:")
        constraints = db.execute(text("""
            SELECT conname, contype 
            FROM pg_constraint 
            WHERE conrelid = 'restaurants'::regclass
        """)).fetchall()
        
        for name, type_code in constraints:
            constraint_type = {'p': 'PRIMARY KEY', 'f': 'FOREIGN KEY', 'u': 'UNIQUE', 'c': 'CHECK'}.get(type_code, type_code)
            print(f"  {name}: {constraint_type}")

if __name__ == '__main__':
    cleanup_phantom_constraint()