# Generated by Django 4.2.28 on 2026-02-24 12:02

import django.db.models.deletion
from django.db import migrations, models


def assign_default_branch(apps, schema_editor):
    Branch = apps.get_model('inventory', 'Branch')
    StockLocation = apps.get_model('inventory', 'StockLocation')

    unassigned = StockLocation.objects.filter(branch__isnull=True)
    if not unassigned.exists():
        return

    default_branch, _ = Branch.objects.get_or_create(
        name='Main Branch',
        defaults={
            'code': None,
            'location': '',
            'contact_phone': '',
            'is_active': True,
        },
    )
    unassigned.update(branch_id=default_branch.id)


def noop_reverse(apps, schema_editor):
    return


class Migration(migrations.Migration):

    dependencies = [
        ('inventory', '0006_branch_stocklocation_branch'),
    ]

    operations = [
        migrations.RunPython(assign_default_branch, noop_reverse),
        migrations.AlterField(
            model_name='stocklocation',
            name='branch',
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name='stock_locations',
                to='inventory.branch',
            ),
        ),
    ]
