# Generated by Django migration system

from django.db import migrations, models


def assign_default_relationships(apps, schema_editor):
    """
    Assign default relationships to existing SCIONLinkAssignment records.
    For existing records without a relationship, we'll default to 'CHILD'
    as it's the most common relationship type.
    """
    SCIONLinkAssignment = apps.get_model('netbox_scion', 'SCIONLinkAssignment')
    
    # Update all existing records to have 'CHILD' relationship
    SCIONLinkAssignment.objects.filter(relationship__isnull=True).update(relationship='CHILD')


def reverse_assign_default_relationships(apps, schema_editor):
    """
    Reverse operation - set relationship to None for all records
    """
    SCIONLinkAssignment = apps.get_model('netbox_scion', 'SCIONLinkAssignment')
    SCIONLinkAssignment.objects.all().update(relationship=None)


class Migration(migrations.Migration):

    dependencies = [
        ('netbox_scion', '0004_add_core_field'),
    ]

    operations = [
        # First, add the field as nullable
        migrations.AddField(
            model_name='scionlinkassignment',
            name='relationship',
            field=models.CharField(
                blank=True,
                choices=[('PARENT', 'PARENT'), ('CHILD', 'CHILD'), ('CORE', 'CORE')],
                help_text='Relationship type of this SCION link',
                max_length=20,
                null=True,
                verbose_name='Relationship'
            ),
        ),
        
        # Run the data migration to assign default values
        migrations.RunPython(
            assign_default_relationships,
            reverse_assign_default_relationships
        ),
        
        # Now make the field non-nullable
        migrations.AlterField(
            model_name='scionlinkassignment',
            name='relationship',
            field=models.CharField(
                choices=[('PARENT', 'PARENT'), ('CHILD', 'CHILD'), ('CORE', 'CORE')],
                help_text='Relationship type of this SCION link',
                max_length=20,
                verbose_name='Relationship'
            ),
        ),
    ]
