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

from django.db import migrations, models


def get_relations(objects, model):
    m2ms = []
    for pk in objects:
        try:
            object = model.objects.get(pk=pk)
            m2ms.append(object)
        except (model.DoesNotExist, model.MultipleObjectsReturned):
            pass
    return m2ms


def forward_migrate(apps, schema_editor):
    RouteMapEntry = apps.get_model('netbox_routing', 'RouteMapEntry')

    PrefixList = apps.get_model('netbox_routing', 'PrefixList')
    CommunityList = apps.get_model('netbox_routing', 'CommunityList')
    Community = apps.get_model('netbox_routing', 'Community')
    ASPath = apps.get_model('netbox_routing', 'ASPath')

    models = (
        ('ipv4', 'match_prefix_list', PrefixList),
        ('ipv6', 'match_prefix_list', PrefixList),
        ('community_list', 'match_community_list', CommunityList),
        ('community', 'match_community', Community),
        ('aspath', 'match_aspath', ASPath),
    )

    for rme in RouteMapEntry.objects.all():
        match = rme.match
        for key, attr, model in models:
            getattr(rme, attr).set(get_relations(rme.match.get(key), model))
            if key in match.keys():
                del match[key]
        rme.save()


def reverse_migrate(apps, schema_editor):
    RouteMapEntry = apps.get_model('netbox_routing', 'RouteMapEntry')

    for rme in RouteMapEntry.objects.all():
        rme.match['ipv4'] = list(
            rme.match_prefix_list.filter(family=4).values_list('pk', flat=True)
        )
        rme.match['ipv6'] = list(
            rme.match_prefix_list.filter(family=6).values_list('pk', flat=True)
        )
        rme.match['aspath'] = list(rme.match_aspath.values_list('pk', flat=True))
        rme.match['community'] = list(rme.match_community.values_list('pk', flat=True))
        rme.match['community_list'] = list(
            rme.match_community_list.values_list('pk', flat=True)
        )
        rme.save()


class Migration(migrations.Migration):

    dependencies = [
        ('netbox_routing', '0025_prefixlistentry_prefix_change'),
    ]

    operations = [
        migrations.AddField(
            model_name='routemapentry',
            name='match_aspath',
            field=models.ManyToManyField(
                blank=True, related_name='route_map_entries', to='netbox_routing.aspath'
            ),
        ),
        migrations.AddField(
            model_name='routemapentry',
            name='match_community',
            field=models.ManyToManyField(
                blank=True,
                related_name='route_map_entries',
                to='netbox_routing.community',
            ),
        ),
        migrations.AddField(
            model_name='routemapentry',
            name='match_community_list',
            field=models.ManyToManyField(
                blank=True,
                related_name='route_map_entries',
                to='netbox_routing.communitylist',
            ),
        ),
        migrations.AddField(
            model_name='routemapentry',
            name='match_prefix_list',
            field=models.ManyToManyField(
                blank=True,
                related_name='route_map_entries',
                to='netbox_routing.prefixlist',
            ),
        ),
        migrations.AddField(
            model_name='routemapentry',
            name='flow_control',
            field=models.PositiveSmallIntegerField(blank=True, null=True),
        ),
        migrations.RunPython(forward_migrate, reverse_code=reverse_migrate),
    ]
