# Generated by Django 5.2.7 on 2025-10-19 15:25

from django.db import migrations, models


def reverse_uid(value: str) -> str:
    normalized = "".join((value or "").split()).upper()
    if not normalized:
        return ""
    if len(normalized) % 2 != 0:
        return normalized[::-1]
    bytes_list = [normalized[index : index + 2] for index in range(0, len(normalized), 2)]
    bytes_list.reverse()
    return "".join(bytes_list)


def populate_reversed_uid(apps, schema_editor):
    RFID = apps.get_model("core", "RFID")
    for tag in RFID.objects.exclude(rfid=""):
        desired = reverse_uid(tag.rfid)
        if tag.reversed_uid != desired:
            RFID.objects.filter(pk=tag.pk).update(reversed_uid=desired)


def noop(apps, schema_editor):
    """No-op reverse migration."""


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0073_assistantprofile_assistant_name"),
    ]

    operations = [
        migrations.AddField(
            model_name="rfid",
            name="reversed_uid",
            field=models.CharField(
                blank=True,
                default="",
                editable=False,
                help_text="UID value stored with opposite endianness for reference.",
                max_length=255,
                verbose_name="Reversed UID",
            ),
        ),
        migrations.RunPython(populate_reversed_uid, reverse_code=noop),
    ]
