#!/usr/bin/env python3
"""
Migration script to update interactable_cooldowns table schema.
Changes from single instance_id to instance_id + action_id composite key.
"""
import asyncio
from api.database import engine
from sqlalchemy import text


async def migrate():
    """Drop and recreate interactable_cooldowns table with new schema."""
    async with engine.begin() as conn:
        print("🔄 Migrating interactable_cooldowns table...")
        
        # Drop old table
        await conn.execute(text("DROP TABLE IF EXISTS interactable_cooldowns CASCADE"))
        print("✅ Dropped old interactable_cooldowns table")
        
        # Create new table with updated schema
        await conn.execute(text("""
            CREATE TABLE interactable_cooldowns (
                id SERIAL PRIMARY KEY,
                interactable_instance_id VARCHAR NOT NULL,
                action_id VARCHAR NOT NULL,
                expiry_timestamp DOUBLE PRECISION NOT NULL,
                CONSTRAINT uix_interactable_action UNIQUE (interactable_instance_id, action_id)
            )
        """))
        print("✅ Created new interactable_cooldowns table with per-action cooldowns")
        
        # Create index for faster lookups
        await conn.execute(text("""
            CREATE INDEX IF NOT EXISTS idx_interactable_cooldowns_expiry 
            ON interactable_cooldowns(expiry_timestamp)
        """))
        print("✅ Created index on expiry_timestamp")
        
    print("✨ Migration complete!")


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