"""
Add performance indexes and constraints to User model.

This migration adds:
1. db_index=True to is_banned and role fields for faster filtering
2. Composite index on (is_banned, role) for common query patterns
3. CheckConstraint to ensure banned users have a ban_date set
"""

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("accounts", "0012_alter_toplist_category_and_more"),
    ]

    operations = [
        # Add db_index to is_banned field
        migrations.AlterField(
            model_name="user",
            name="is_banned",
            field=models.BooleanField(default=False, db_index=True),
        ),
        # Add composite index for common query patterns
        migrations.AddIndex(
            model_name="user",
            index=models.Index(fields=["is_banned", "role"], name="accounts_user_banned_role_idx"),
        ),
        # Add CheckConstraint for ban consistency
        migrations.AddConstraint(
            model_name="user",
            constraint=models.CheckConstraint(
                name="user_ban_consistency",
                check=models.Q(is_banned=False) | models.Q(ban_date__isnull=False),
                violation_error_message="Banned users must have a ban_date set",
            ),
        ),
    ]
