# Generated by Django 4.2.20 on 2025-05-04 21:51

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


def set_character_from_planet(apps, schema_editor):
    CharacterPlanetDetails = apps.get_model("ledger", "CharacterPlanetDetails")
    CharacterPlanet = apps.get_model("ledger", "CharacterPlanet")

    for detail in CharacterPlanetDetails.objects.all():
        if detail.planet_id:
            try:
                planet = CharacterPlanet.objects.get(id=detail.planet_id)
                detail.character = planet.character
                detail.save()
            except CharacterPlanet.DoesNotExist:
                detail.delete()
                detail.save()
        else:
            detail.delete()
            detail.save()


class Migration(migrations.Migration):

    dependencies = [
        ("ledger", "0009_remove_characteraudit_last_update_mining_and_more"),
    ]

    operations = [
        migrations.RunPython(set_character_from_planet),
        migrations.AlterField(
            model_name="characterplanetdetails",
            name="character",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="ledger_character_planet_details",
                to="ledger.characteraudit",
            ),
        ),
    ]
