#!/usr/bin/env python3
"""
Fix telegram_id column to be nullable for web users.
"""
import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text

# Database connection
DB_USER = os.getenv("POSTGRES_USER", "echoes_user")
DB_PASS = os.getenv("POSTGRES_PASSWORD", "echoes_pass")
DB_NAME = os.getenv("POSTGRES_DB", "echoes_db")
DB_HOST = os.getenv("POSTGRES_HOST", "echoes_of_the_ashes_db")
DB_PORT = os.getenv("POSTGRES_PORT", "5432")

DATABASE_URL = f"postgresql+psycopg://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

async def fix_telegram_id():
    """Alter telegram_id column to be nullable"""
    engine = create_async_engine(DATABASE_URL, echo=True)
    
    try:
        async with engine.begin() as conn:
            print("Making telegram_id nullable...")
            await conn.execute(text(
                "ALTER TABLE players ALTER COLUMN telegram_id DROP NOT NULL;"
            ))
            print("✅ telegram_id is now nullable!")
            
    except Exception as e:
        print(f"❌ Error: {e}")
    finally:
        await engine.dispose()

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