# Generated by Django 5.2.10 on 2026-01-25 23:16
from tqdm import tqdm
from django.db import migrations, transaction

from edc_pharmacy.models import Confirmation, StorageBin


def update_storage_bin_container(apps, schema_editor):
    qs = StorageBin.objects.filter(storagebinitem__isnull=False, container__isnull=True)
    for obj in tqdm(qs, total=qs.count()):
        obj.container = obj.storagebinitem_set.all().first().stock.container
        obj.save(update_fields=["container"])


def update_pharmacy(apps, schema_editor):
    with transaction.atomic():
        qs = Confirmation.objects.all()
        for obj in tqdm(qs, total=qs.count()):
            obj.save()


class Migration(migrations.Migration):

    dependencies = [
        ("edc_pharmacy", "0132_historicalstoragebin_container_storagebin_container"),
    ]

    operations = [
        migrations.RunPython(update_pharmacy),
        migrations.RunPython(update_storage_bin_container),
    ]
