# Generated by Django 6.0 on 2026-01-19 17:11

from django.db import migrations
from sequences.models import Sequence
from tqdm import tqdm

from edc_pharmacy.models import Allocation, ConfirmationAtLocationItem, DispenseItem, Stock, \
    StockTransferItem, StorageBinItem


def update_sequences_for_model_renames(apps, schema_editor):
    Sequence.objects.filter(name="edc_pharmacy.confirmationatsiteitem").update(
        name="edc_pharmacy.confirmationatlocationitem"
    )
    Sequence.objects.filter(name="edc_pharmacy.confirmationatsite").update(
        name="edc_pharmacy.confirmationatlocation"
    )


def update_confirmation_at_location(apps, schema_editor):
    qs = ConfirmationAtLocationItem.objects.filter(stock_transfer_item__isnull=True)
    for obj in tqdm(qs, total=qs.count()):
        stock_obj = Stock.objects.get(code=obj.stock.code)
        if stock_obj.stocktransferitem_set.all().count() == 1:
            obj.stock_transfer_item = stock_obj.stocktransferitem_set.all()[0]
        obj.save()


def update_stock_codes_on_item_models(apps, schema_editor):
    qs = StockTransferItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()
    qs = StorageBinItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()


def update_subject_identifier(apps, schema_editor):
    Allocation.objects.filter(stock__isnull=True).delete()
    qs = Allocation.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.subject_identifier = obj.registered_subject.subject_identifier
        obj.code = obj.stock.code
        obj.save(update_fields=["subject_identifier", "code"])


def update_stock_boolean_fields(apps, schema_editor):

    print("\nUpdating boolean fields on the Stock model ...")
    print("  iterating over 1 / 4 models (StockTransferItem)...")
    qs = StockTransferItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()

    print("  iterating over 2 / 4 models (ConfirmationAtLocationItem)...")
    qs = ConfirmationAtLocationItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()

    print("  iterating over 3 / 4 models (StorageBinItem)...")
    qs = StorageBinItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()

    print("  iterating over 4 / 4 models (DispenseItem)...")
    qs = DispenseItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()
    print("Done.")


def update_dispense_item_stock_code(apps, schema_editor):
    qs = DispenseItem.objects.all()
    for obj in tqdm(qs, total=qs.count()):
        obj.save()


class Migration(migrations.Migration):
    dependencies = [
        ("edc_pharmacy", "0105_allocation_code_historicalallocation_code"),
    ]

    operations = [
        migrations.RunPython(update_sequences_for_model_renames),
        migrations.RunPython(update_confirmation_at_location),
        migrations.RunPython(update_stock_codes_on_item_models),
        migrations.RunPython(update_subject_identifier),
        migrations.RunPython(update_stock_boolean_fields),
        migrations.RunPython(update_dispense_item_stock_code),
    ]
