#!/usr/bin/env python3
"""
Rename tables in system schema to deprecated to avoid extra detection.
"""
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text

async def rename():
    from app.core.config import settings
    engine = create_async_engine(str(settings.SQLALCHEMY_DATABASE_URI))
    
    async with engine.begin() as conn:
        # Check if tables exist
        result = await conn.execute(text("""
            SELECT table_schema, table_name 
            FROM information_schema.tables 
            WHERE table_schema = 'system' AND table_name IN ('competitions', 'user_scores');
        """))
        rows = result.fetchall()
        print("Tables to rename:")
        for row in rows:
            print(f"  {row.table_schema}.{row.table_name}")
        
        # Rename competitions
        try:
            await conn.execute(text('ALTER TABLE system.competitions RENAME TO competitions_deprecated;'))
            print("Renamed system.competitions -> system.competitions_deprecated")
        except Exception as e:
            print(f"Error renaming competitions: {e}")
        
        # Rename user_scores
        try:
            await conn.execute(text('ALTER TABLE system.user_scores RENAME TO user_scores_deprecated;'))
            print("Renamed system.user_scores -> system.user_scores_deprecated")
        except Exception as e:
            print(f"Error renaming user_scores: {e}")
        
        # Verify
        result = await conn.execute(text("""
            SELECT table_schema, table_name 
            FROM information_schema.tables 
            WHERE table_schema = 'system' AND table_name LIKE '%deprecated';
        """))
        rows = result.fetchall()
        print("\nAfter rename:")
        for row in rows:
            print(f"  {row.table_schema}.{row.table_name}")
    
    await engine.dispose()

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