# Generated by Django 5.2.5 on 2026-02-13 21:04

import django.core.validators
import django.db.models.deletion
import netbox.models.deletion
import taggit.managers
import utilities.json
from django.db import migrations, models


def forward_migrate(apps, schema_editor):
    BGPPeer = apps.get_model('netbox_routing', 'BGPPeer')
    BGPSessionTemplate = apps.get_model('netbox_routing', 'BGPSessionTemplate')
    BFDProfile = apps.get_model('netbox_routing', 'BFDPRofile')

    for peer in BGPPeer.objects.all():
        if peer.bfd_enabled:
            try:
                bfd = BFDProfile.objects.get(name='BFD Enabled (default profile)')
            except BFDProfile.DoesNotExist:
                bfd = BFDProfile.objects.create(name='BFD Enabled (default profile)')
            peer.bfd = bfd
            peer.save()

    for peer in BGPSessionTemplate.objects.all():
        if peer.bfd_enabled:
            try:
                bfd = BFDProfile.objects.get(name='BFD Enabled (default profile)')
            except BFDProfile.DoesNotExist:
                bfd = BFDProfile.objects.create(name='BFD Enabled (default profile)')
            peer.bfd = bfd
            peer.save()


def reverse_migrate(apps, schema_editor):
    BFDProfile = apps.get_model('netbox_routing', 'BFDPRofile')
    for profile in BFDProfile.objects.all():
        for peer in profile.bgp_peers.all():
            peer.bfd_enabled = True
            peer.save()
        for peer in profile.bgp_session_templates.all():
            peer.bfd_enabled = True
            peer.save()


class Migration(migrations.Migration):

    dependencies = [
        ('extras', '0134_owner'),
        ('netbox_routing', '0023_peer_and_session_add_ttl'),
        ('tenancy', '0022_add_comments_to_organizationalmodel'),
        ('users', '0015_owner'),
    ]

    operations = [
        migrations.CreateModel(
            name='BFDProfile',
            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)),
                ('name', models.CharField(max_length=100)),
                (
                    'min_tx_int',
                    models.PositiveIntegerField(
                        blank=True,
                        null=True,
                    ),
                ),
                (
                    'min_rx_int',
                    models.PositiveIntegerField(
                        blank=True,
                        null=True,
                    ),
                ),
                (
                    'multiplier',
                    models.PositiveSmallIntegerField(
                        blank=True,
                        null=True,
                    ),
                ),
                (
                    'hold',
                    models.PositiveIntegerField(
                        blank=True,
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(60),
                            django.core.validators.MaxValueValidator(60000),
                        ],
                    ),
                ),
                (
                    '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'
                    ),
                ),
                (
                    'tenant',
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name='bfd_profiles',
                        to='tenancy.tenant',
                    ),
                ),
            ],
            options={
                'verbose_name': 'BFD Profile',
                'verbose_name_plural': 'BFD Profiles',
                'ordering': ('name',),
            },
            bases=(netbox.models.deletion.DeleteMixin, models.Model),
        ),
        migrations.RenameField(
            model_name='bgppeer',
            old_name='bfd',
            new_name='bfd_enabled',
        ),
        migrations.RenameField(
            model_name='bgpsessiontemplate',
            old_name='bfd',
            new_name='bfd_enabled',
        ),
        migrations.AddField(
            model_name='bgppeer',
            name='bfd',
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name='bgp_peers',
                to='netbox_routing.bfdprofile',
            ),
        ),
        migrations.AddField(
            model_name='bgpsessiontemplate',
            name='bfd',
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name='bgp_session_templates',
                to='netbox_routing.bfdprofile',
            ),
        ),
        migrations.AddConstraint(
            model_name='bfdprofile',
            constraint=models.UniqueConstraint(
                fields=('name',), name='netbox_routing_bfdprofile_name'
            ),
        ),
        migrations.RunPython(code=forward_migrate, reverse_code=reverse_migrate),
        migrations.AlterField(
            model_name='bfdprofile',
            name='min_rx_int',
            field=models.PositiveIntegerField(
                default=0,
                validators=[
                    django.core.validators.MinValueValidator(60),
                    django.core.validators.MaxValueValidator(60000),
                ],
            ),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='bfdprofile',
            name='min_tx_int',
            field=models.PositiveIntegerField(
                default=0,
                validators=[
                    django.core.validators.MinValueValidator(60),
                    django.core.validators.MaxValueValidator(60000),
                ],
            ),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='bfdprofile',
            name='multiplier',
            field=models.PositiveSmallIntegerField(
                default=0,
                validators=[
                    django.core.validators.MinValueValidator(0),
                    django.core.validators.MaxValueValidator(255),
                ],
            ),
            preserve_default=False,
        ),
        migrations.RemoveField(
            model_name='bgppeer',
            name='bfd_enabled',
        ),
        migrations.RemoveField(
            model_name='bgpsessiontemplate',
            name='bfd_enabled',
        ),
    ]
