# Generated by Django 4.2.28 on 2026-03-03 17:01

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


def on_migrate(apps, schema_editor):
    try:
        OldEveEntity = apps.get_model("eveuniverse", "EveEntity")
    except LookupError:
        print("EVE Universe Model not found skipping migration.")
        return
    EveEntity = apps.get_model("killstats", "EveEntity")
    batch_size = 1000
    old_entities = OldEveEntity.objects.exclude(
        id__in=EveEntity.objects.values_list("id", flat=True)
    ).only("id", "name", "category")
    migrated = 0
    bulk_list = []

    for old_entity in old_entities.iterator(chunk_size=batch_size):
        bulk_list.append(
            EveEntity(
                id=old_entity.id,
                name=old_entity.name,
                category=old_entity.category,
            )
        )

        if len(bulk_list) >= batch_size:
            created = EveEntity.objects.bulk_create(
                bulk_list, ignore_conflicts=True, batch_size=batch_size
            )
            migrated += len(created)
            bulk_list = []

    if bulk_list:
        created = EveEntity.objects.bulk_create(
            bulk_list, ignore_conflicts=True, batch_size=batch_size
        )
        migrated += len(created)


class Migration(migrations.Migration):

    dependencies = [
        ("eve_sde", "0014_dogmaeffect_description_de_and_more"),
        ("killstats", "0007_eveentity"),
    ]

    operations = [
        migrations.RunPython(on_migrate),
        migrations.AlterField(
            model_name="attacker",
            name="alliance",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="attacker_alliance",
                to="killstats.eveentity",
            ),
        ),
        migrations.AlterField(
            model_name="attacker",
            name="character",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="attacker_character",
                to="killstats.eveentity",
            ),
        ),
        migrations.AlterField(
            model_name="attacker",
            name="corporation",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="attacker_corp",
                to="killstats.eveentity",
            ),
        ),
        migrations.AlterField(
            model_name="attacker",
            name="ship",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="attacker_ship",
                to="eve_sde.itemtype",
            ),
        ),
        migrations.AlterField(
            model_name="killmail",
            name="victim",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="killstats.eveentity",
            ),
        ),
        migrations.AlterField(
            model_name="killmail",
            name="victim_ship",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="eve_sde.itemtype",
            ),
        ),
    ]
