# Generated by Django 5.2.10 on 2026-01-24 22:03
from decimal import Decimal

from django.db import migrations
from django.db import transaction
from tqdm import tqdm

from edc_pharmacy.models import Container, OrderItem, ReceiveItem, Stock
from django.db.models import F


def update_pharmacy(apps, schema_editor):

    # update new container_unit_qty from container default qty
    # before dropping container default qty
    with transaction.atomic():
        for obj in OrderItem.objects.all():
            obj.container_unit_qty = obj.container.unit_qty_default
            obj.save_base(update_fields=["container_unit_qty"])

    # update new container_unit_qty from container default qty
    # before dropping container default qty
    with transaction.atomic():
        for obj in ReceiveItem.objects.all():
            obj.container_unit_qty = obj.container.unit_qty_default
            obj.save_base(update_fields=["container_unit_qty"])

    # update mising stock unit_qty_in from container default qty
    # before dropping container default qty
    with transaction.atomic():
        qs = Stock.objects.exclude(unit_qty_in=F("container__unit_qty_default"))
        for obj in tqdm(qs, total=qs.count()):
            obj.unit_qty_in = obj.container.unit_qty_default
            obj.save_base(update_fields=["unit_qty_in"])

    # consolidate 'bucket' containers
    with transaction.atomic():
        try:
            container = Container.objects.get(name="BUCKET 16000")
        except Container.DoesNotExist:
            pass
        else:
            container.name = "bucket_of_tablets"
            container.display_name = "Bucket of tablets (Max. 30000)"
            container.save()
            container.refresh_from_db()

            for name in [
                "bucket_11978",
                "bucket_14664",
                "bucket_16550",
                "bucket_9195",
                "BUCKET-16000",
                "bottle30k"
            ]:
                OrderItem.objects.filter(
                    container__name=name,
                    container_unit_qty=F("container__unit_qty_default")
                ).update(container=container)
                ReceiveItem.objects.filter(
                    container__name=name,
                    container_unit_qty=F("container__unit_qty_default")
                ).update(container=container)
                Stock.objects.filter(
                    container__name=name,
                    unit_qty_in=F("container__unit_qty_default"),
                    container_unit_qty=F("container__unit_qty_default")
                ).update(container=container)

        Stock.objects.filter(
            container_unit_qty__isnull=True, container__name="bottle128"
            ).update(container_unit_qty=Decimal("128.0"))
        Stock.objects.filter(container_unit_qty__isnull=True
            ).update(container_unit_qty=F("unit_qty_in"))


class Migration(migrations.Migration):

    dependencies = [
        ("edc_pharmacy", "0119_alter_container_max_items_per_subject_and_more"),
    ]

    operations = [migrations.RunPython(update_pharmacy)]
