# Generated by Django 5.2.5 on 2026-02-14 17:47

import django.db.models.deletion
import ipam.fields
import netbox.models.deletion
import taggit.managers
import utilities.json
from django.db import migrations, models


def forward_migrate(apps, schema_editor):
    from django.contrib.contenttypes.models import ContentType

    PrefixListEntry = apps.get_model('netbox_routing', 'PrefixListEntry')
    CustomPrefix = apps.get_model('netbox_routing', 'CustomPrefix')
    Prefix = apps.get_model('ipam', 'Prefix')

    pfx_ct = ContentType.objects.get_for_model(Prefix)
    cpfx_ct = ContentType.objects.get_for_model(CustomPrefix)

    for ple in PrefixListEntry.objects.all():
        if ple.prefix:
            try:
                prefix = Prefix.objects.get(prefix=ple.prefix)
                ple.assigned_prefix_type_id = pfx_ct.pk
                ple.assigned_prefix_id = prefix.pk
                ple.save()
            except (Prefix.DoesNotExist, Prefix.MultipleObjectsReturned):
                try:
                    prefix = CustomPrefix.objects.get(prefix=ple.prefix)
                except CustomPrefix.DoesNotExist:
                    prefix = CustomPrefix.objects.create(prefix=ple.prefix)
                ple.assigned_prefix_type_id = cpfx_ct.pk
                ple.assigned_prefix_id = prefix.pk
                ple.save()


def reverse_migrate(apps, schema_editor):
    PrefixListEntry = apps.get_model('netbox_routing', 'PrefixListEntry')
    Prefix = apps.get_model('ipam', 'Prefix')
    CustomPrefix = apps.get_model('netbox_routing', 'CustomPrefix')
    ContentType = apps.get_model('contenttypes', 'ContentType')

    pfx_ct = ContentType.objects.get(app_label='ipam', model='prefix')
    cpfx_ct = ContentType.objects.get(app_label='netbox_routing', model='customprefix')

    for ple in PrefixListEntry.objects.all():
        if ple.assigned_prefix_type == pfx_ct:
            prefix = Prefix.objects.get(pk=ple.assigned_prefix_id)
            ple.prefix = prefix.prefix
            ple.save()
        elif ple.assigned_prefix_type == cpfx_ct:
            prefix = CustomPrefix.objects.get(pk=ple.assigned_prefix_id)
            ple.prefix = prefix.prefix
            ple.save()


class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('extras', '0134_owner'),
        ('netbox_routing', '0024_bfdprofile'),
        ('users', '0015_owner'),
    ]

    operations = [
        migrations.CreateModel(
            name='CustomPrefix',
            fields=[
                (
                    'id',
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False
                    ),
                ),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('last_updated', models.DateTimeField(auto_now=True, null=True)),
                (
                    'custom_field_data',
                    models.JSONField(
                        blank=True,
                        default=dict,
                        encoder=utilities.json.CustomFieldJSONEncoder,
                    ),
                ),
                ('description', models.CharField(blank=True, max_length=200)),
                ('comments', models.TextField(blank=True)),
                ('prefix', ipam.fields.IPNetworkField()),
                (
                    'owner',
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        to='users.owner',
                    ),
                ),
                (
                    'tags',
                    taggit.managers.TaggableManager(
                        through='extras.TaggedItem', to='extras.Tag'
                    ),
                ),
            ],
            options={
                'ordering': ['prefix'],
                'constraints': [
                    models.UniqueConstraint(
                        fields=('prefix',),
                        name='netbox_routing_customprefix_unique_prefix',
                        violation_error_message='Prefix must be unique.',
                    )
                ],
            },
            bases=(netbox.models.deletion.DeleteMixin, models.Model),
        ),
        migrations.AddField(
            model_name='prefixlistentry',
            name='assigned_prefix_id',
            field=models.PositiveBigIntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='prefixlistentry',
            name='assigned_prefix_type',
            field=models.ForeignKey(
                blank=True,
                limit_choices_to=models.Q(
                    models.Q(
                        models.Q(('app_label', 'ipam'), ('model', 'prefix')),
                        models.Q(
                            ('app_label', 'netbox_routing'),
                            ('model', 'customprefix'),
                        ),
                        _connector='OR',
                    )
                ),
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name='+',
                to='contenttypes.contenttype',
            ),
        ),
        migrations.AlterField(
            model_name='prefixlistentry',
            name='prefix',
            field=ipam.fields.IPNetworkField(blank=True, null=True),
        ),
        migrations.RunPython(forward_migrate, reverse_code=reverse_migrate),
        migrations.RemoveField(
            model_name='prefixlistentry',
            name='prefix',
        ),
    ]
