# 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):
    Game = apps.get_model(
        "games", "Game"
    )  # Replace 'your_app_name' with the actual name of your app

    for game in Game.objects.all():
        name = game.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
        game.sort_name = sort_name
        game.save()


class Migration(migrations.Migration):
    dependencies = [
        ("games", "0024_edition_sort_name"),
    ]

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