# Fix for MySQL 5.7: fcm_token index without key length
from django.db import migrations


def drop_fcm_token_indexes(apps, schema_editor):
    """Drop any indexes on fcm_token column that don't have key length"""
    db_alias = schema_editor.connection.alias
    with schema_editor.connection.cursor() as cursor:
        # Get all indexes on fcm_token column
        cursor.execute("""
            SELECT INDEX_NAME 
            FROM information_schema.statistics 
            WHERE table_schema = DATABASE() 
            AND table_name = 'user_user' 
            AND column_name = 'fcm_token'
        """)
        indexes = cursor.fetchall()
        
        # Drop each index
        for (index_name,) in indexes:
            try:
                cursor.execute(f"ALTER TABLE `user_user` DROP INDEX `{index_name}`")
            except Exception:
                # Index might not exist or might be a primary key
                pass


def reverse_drop_fcm_token_indexes(apps, schema_editor):
    """Reverse migration - do nothing"""
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('user', '0005_user_compliance_enabled'),
    ]

    operations = [
        migrations.RunPython(
            drop_fcm_token_indexes,
            reverse_drop_fcm_token_indexes,
        ),
    ]

