# Generated manually to update status field choices and normalize data

from django.db import migrations, models


def normalize_statuses_forward(apps, schema_editor):
    Tasks = apps.get_model('writer', 'Tasks')
    Content = apps.get_model('writer', 'Content')

    # Normalize task statuses to queued/completed
    queued_states = {'queued', 'in_progress'}
    completed_states = {'draft', 'review', 'published', 'completed'}

    Tasks.objects.filter(status__in=queued_states).update(status='queued')
    Tasks.objects.filter(status__in=completed_states).update(status='completed')

    # Normalize content statuses: published -> publish (new choice)
    Content.objects.filter(status='published').update(status='publish')


def normalize_statuses_backward(apps, schema_editor):
    Tasks = apps.get_model('writer', 'Tasks')
    Content = apps.get_model('writer', 'Content')

    # Restore pre-normalization values
    Tasks.objects.filter(status='queued').update(status='queued')
    Tasks.objects.filter(status='completed').update(status='published')

    Content.objects.filter(status='publish').update(status='published')


class Migration(migrations.Migration):

    dependencies = [
        ('writer', '0005_move_content_fields_to_content'),
    ]

    operations = [
        migrations.RunPython(normalize_statuses_forward, normalize_statuses_backward),
        migrations.AlterField(
            model_name='tasks',
            name='status',
            field=models.CharField(
                choices=[('queued', 'Queued'), ('completed', 'Completed')],
                default='queued',
                max_length=50,
            ),
        ),
        migrations.AlterField(
            model_name='content',
            name='status',
            field=models.CharField(
                choices=[('draft', 'Draft'), ('review', 'Review'), ('publish', 'Publish')],
                default='draft',
                help_text='Content workflow status (draft, review, publish)',
                max_length=50,
            ),
        ),
    ]

