# Добавляем поле ai_draft_submissions в модель Homework (черновики от ИИ)

from django.db import migrations, models


def fill_ai_draft_submissions(apps, schema_editor):
    """Заполнить ai_draft_submissions для существующих заданий."""
    Homework = apps.get_model('homework', 'Homework')
    for hw in Homework.objects.all():
        count = hw.submissions.filter(status='pending').exclude(ai_checked_at__isnull=True).count()
        hw.ai_draft_submissions = count
        hw.save(update_fields=['ai_draft_submissions'])


class Migration(migrations.Migration):

    dependencies = [
        ('homework', '0011_homeworksubmission_graded_by_ai'),
    ]

    operations = [
        migrations.AddField(
            model_name='homework',
            name='ai_draft_submissions',
            field=models.IntegerField(
                default=0,
                help_text='Количество решений со статусом «ожидает проверки» и заполненным черновиком от ИИ',
                verbose_name='Черновиков от ИИ',
            ),
        ),
        migrations.RunPython(fill_ai_draft_submissions, migrations.RunPython.noop),
    ]
