# Generated by Django 5.2.8 on 2025-11-13 00:09

import django.core.validators
from django.conf import settings
from django.db import migrations, models


def _normalize_network_fields(apps, schema_editor):
    """Replace ``NULL`` values with empty strings for legacy records."""

    Node = apps.get_model("nodes", "Node")
    ContentSample = apps.get_model("nodes", "ContentSample")

    Node.objects.filter(ipv4_address__isnull=True).update(ipv4_address="")
    Node.objects.filter(ipv6_address__isnull=True).update(ipv6_address="")
    Node.objects.filter(constellation_ip__isnull=True).update(constellation_ip="")
    Node.objects.filter(address__isnull=True).update(address="")
    Node.objects.filter(constellation_device__isnull=True).update(
        constellation_device=""
    )
    Node.objects.filter(mac_address__isnull=True).update(mac_address="")

    ContentSample.objects.filter(hash__isnull=True).update(hash="")


def _restore_network_nulls(apps, schema_editor):
    """Recreate the legacy ``NULL`` values when rolling back."""

    Node = apps.get_model("nodes", "Node")
    ContentSample = apps.get_model("nodes", "ContentSample")

    Node.objects.filter(ipv4_address="").update(ipv4_address=None)
    Node.objects.filter(ipv6_address="").update(ipv6_address=None)
    Node.objects.filter(constellation_ip="").update(constellation_ip=None)
    Node.objects.filter(address="").update(address=None)
    Node.objects.filter(constellation_device="").update(constellation_device=None)
    Node.objects.filter(mac_address="").update(mac_address=None)

def _fill_blank_strings(apps, schema_editor):
    """Ensure legacy ``NULL`` values become empty strings before constraints."""

    Node = apps.get_model("nodes", "Node")
    ContentSample = apps.get_model("nodes", "ContentSample")

    Node.objects.filter(ipv4_address__isnull=True).update(ipv4_address="")
    Node.objects.filter(ipv6_address__isnull=True).update(ipv6_address="")
    Node.objects.filter(constellation_ip__isnull=True).update(constellation_ip="")
    Node.objects.filter(address__isnull=True).update(address="")
    Node.objects.filter(constellation_device__isnull=True).update(
        constellation_device="",
    )
    Node.objects.filter(mac_address__isnull=True).update(mac_address="")

    ContentSample.objects.filter(hash__isnull=True).update(hash="")


def _noop(apps, schema_editor):
    """No-op reverse migration."""
    # Keeping legacy null values is acceptable; future migrations enforce blanks.
    return None


class Migration(migrations.Migration):

    dependencies = [
        ("nodes", "0036_alter_node_ipv4_address"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RunPython(
            _normalize_network_fields,
            _restore_network_nulls,
        ),
        migrations.AlterField(
            model_name="contentsample",
            name="hash",
            field=models.CharField(blank=True, max_length=64),
        ),
        migrations.AlterField(
            model_name="node",
            name="constellation_device",
            field=models.CharField(blank=True, max_length=16),
        ),
        migrations.AlterField(
            model_name="node",
            name="mac_address",
            field=models.CharField(blank=True, max_length=17),
        ),
        migrations.AlterField(
            model_name="node",
            name="ipv4_address",
            field=models.TextField(blank=True),
        ),
        migrations.AlterField(
            model_name="node",
            name="ipv6_address",
            field=models.CharField(
                blank=True,
                max_length=39,
                validators=[django.core.validators.validate_ipv6_address],
            ),
        ),
        migrations.AlterField(
            model_name="node",
            name="constellation_ip",
            field=models.CharField(
                blank=True,
                max_length=45,
                validators=[django.core.validators.validate_ipv46_address],
            ),
        ),
        migrations.AlterField(
            model_name="node",
            name="address",
            field=models.CharField(
                blank=True,
                max_length=45,
                validators=[django.core.validators.validate_ipv46_address],
            ),
        ),
        migrations.AddConstraint(
            model_name="node",
            constraint=models.UniqueConstraint(
                condition=~models.Q(constellation_device=""),
                fields=("constellation_device",),
                name="nodes_node_constellation_device_unique",
            ),
        ),
        migrations.AddConstraint(
            model_name="node",
            constraint=models.UniqueConstraint(
                condition=~models.Q(mac_address=""),
                fields=("mac_address",),
                name="nodes_node_mac_address_unique",
            ),
        ),
        migrations.AddConstraint(
            model_name="contentsample",
            constraint=models.UniqueConstraint(
                condition=~models.Q(hash=""),
                fields=("hash",),
                name="nodes_contentsample_hash_unique",
            ),
        ),
    ]
