"""One-shot data fix: normalize legacy uppercase 'GRADED' rows to lowercase.

Context: Submission.status choices are strictly lowercase
('pending'|'graded'|'reviewed'|'failed'). Two historical rows contain the
uppercase value 'GRADED' and are invisible to pipeline dispatchers that filter
on status='graded'. This migration normalizes them.

Idempotent: running twice is a no-op. Reverse is a deliberate no-op — we never
want to reintroduce the bug.
"""
from django.db import migrations


def normalize_graded_status(apps, schema_editor):
    Submission = apps.get_model('exams', 'Submission')
    updated = Submission.objects.filter(status='GRADED').update(status='graded')
    print(f'[0112] normalized {updated} row(s) GRADED -> graded')


def reverse_normalize(apps, schema_editor):
    """No-op by design. Never reintroduce the bug."""
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('exams', '0111_alter_taskdelegation_task_type'),
    ]

    operations = [
        migrations.RunPython(normalize_graded_status, reverse_normalize),
    ]
