import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'backend'))

from sqlalchemy import create_engine, text
from backend.config import ADMIN_CONFIG

def run_migration():
    """Run the booking system migration script"""
    try:
        # Create database engine
        engine = create_engine(ADMIN_CONFIG['DATABASE_URL'])
        
        # Read the migration script
        with open('database/scripts/09_complete_booking_migration.sql', 'r') as f:
            sql_script = f.read()
        
        # Execute the migration script
        with engine.connect() as conn:
            # Split the script into individual statements
            statements = sql_script.split(';')
            for i, statement in enumerate(statements):
                statement = statement.strip()
                if statement:
                    try:
                        conn.execute(text(statement))
                        print(f"Executed statement {i+1}: {statement[:50]}...")
                    except Exception as e:
                        print(f"Error executing statement {i+1}: {e}")
                        print(f"Statement: {statement}")
                        # Continue with the next statement
                        continue
            
            conn.commit()
        
        print("Migration script executed successfully!")
        
    except Exception as e:
        print(f"Error running migration: {e}")

if __name__ == '__main__':
    run_migration()