# Generated by Django 5.1.13 on 2025-12-16 15:51

from django.db import migrations, models


def update_thread_stats(apps, schema_editor):
    """
    For all threads with trashed messages, update the thread stats
    to set the is_trashed flag to True if needed.
    """
    Thread = apps.get_model('core', 'Thread')

    non_trashed_messages_counter = models.Count('messages', filter=models.Q(messages__is_trashed=False))
    threads_to_update = (
        Thread.objects
        .annotate(non_trashed_messages_count=non_trashed_messages_counter)
        .filter(has_trashed=True, non_trashed_messages_count=0)
    )
    threads_to_update.update(is_trashed=True)

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0012_maildomain_custom_settings_alter_user_language_and_more'),
    ]

    operations = [
        migrations.AddField(
            model_name='thread',
            name='is_trashed',
            field=models.BooleanField(default=False, help_text='Whether all messages in the thread are trashed', verbose_name='is trashed'),
        ),
        migrations.RunPython(update_thread_stats, reverse_code=migrations.RunPython.noop),
    ]
