from sqlalchemy import create_engine, text
import sys
import os

# Add backend path
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))
from backend.config import settings

def migrate():
    engine = create_engine(settings.DATABASE_URL)
    with engine.connect() as conn:
        try:
            # Check if column exists
            print("Checking schema...")
            # SQLite specific pragma
            result = conn.execute(text("PRAGMA table_info(companies)"))
            columns = [row[1] for row in result.fetchall()]
            
            if "ai_opener" in columns:
                print("Column 'ai_opener' already exists. Skipping.")
            else:
                print("Adding column 'ai_opener' to 'companies' table...")
                conn.execute(text("ALTER TABLE companies ADD COLUMN ai_opener TEXT"))
                conn.commit()
                print("✅ Migration successful.")
                
        except Exception as e:
            print(f"❌ Migration failed: {e}")

if __name__ == "__main__":
    migrate()
