#!/usr/bin/env python3
"""
Add image_url column to mtg_sets table in the database.
"""

import asyncio
import sys
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import text

sys.path.insert(0, '/home/wall-o/projects/mtgonline/backend')

from app.core.settings import get_settings


async def add_image_url_column():
    """Add image_url column to mtg_sets table."""
    settings = get_settings()
    engine = create_async_engine(settings.MTG_DATABASE_URL)
    async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
    
    async with async_session() as session:
        # Check if column already exists
        result = await session.execute(text("""
            SELECT column_name FROM information_schema.columns 
            WHERE table_name = 'mtg_sets' AND column_name = 'image_url'
        """))
        existing = result.fetchone()
        
        if existing:
            print("✓ image_url column already exists in mtg_sets table")
            return
        
        # Add the column
        print("Adding image_url column to mtg_sets table...")
        await session.execute(text("""
            ALTER TABLE mtg_sets 
            ADD COLUMN image_url TEXT
        """))
        print("✓ image_url column added successfully")
        
        # Verify
        result = await session.execute(text("""
            SELECT column_name FROM information_schema.columns 
            WHERE table_name = 'mtg_sets' AND column_name = 'image_url'
        """))
        verified = result.fetchone()
        if verified:
            print("✓ Column verified in database")
        else:
            print("✗ Column not found after addition")
    
    await engine.dispose()


if __name__ == "__main__":
    asyncio.run(add_image_url_column())
