# Generated by Django 5.1.4 on 2025-01-26 14:32

import django.db.models.deletion
from django.db import migrations, models


def shop_from_property(apps, schema_editor):
    rolling_stock = apps.get_model("roster", "RollingStock")
    shop_model = apps.get_model("metadata", "Shop")
    for row in rolling_stock.objects.all():
        property = row.property.filter(
            property__name__icontains="shop"
        ).first()
        if property:
            shop, created = shop_model.objects.get_or_create(
                name=property.value,
                defaults={"on_line": False}
            )

            row.shop = shop
            row.save()


class Migration(migrations.Migration):

    dependencies = [
        ("metadata", "0023_shop"),
        ("roster", "0033_rename_manufacturer_rollingclass_manufacturer_old"),
    ]

    operations = [
        migrations.AddField(
            model_name="rollingstock",
            name="shop",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="metadata.shop",
            ),
        ),
        migrations.RunPython(
            shop_from_property,
            reverse_code=migrations.RunPython.noop
        ),
    ]
