# Generated by Django 4.1.5 on 2023-11-09 09:32

from django.db import migrations, models


def create_sort_name(apps, schema_editor):
    Edition = apps.get_model(
        "games", "Edition"
    )  # Replace 'your_app_name' with the actual name of your app

    for edition in Edition.objects.all():
        name = edition.name
        # Check for articles at the beginning of the name and move them to the end
        if name.lower().startswith("the "):
            sort_name = f"{name[4:]}, The"
        elif name.lower().startswith("a "):
            sort_name = f"{name[2:]}, A"
        elif name.lower().startswith("an "):
            sort_name = f"{name[3:]}, An"
        else:
            sort_name = name
        # Save the sort_name back to the database
        edition.sort_name = sort_name
        edition.save()


class Migration(migrations.Migration):
    dependencies = [
        ("games", "0023_purchase_date_finished"),
    ]

    operations = [
        migrations.AddField(
            model_name="edition",
            name="sort_name",
            field=models.CharField(blank=True, default=None, max_length=255, null=True),
        ),
        migrations.RunPython(create_sort_name),
    ]
