# Generated by Django 4.2.23 on 2025-12-29 22:41

# Django
from django.db import migrations


def populate_order_references(apps, schema_editor):
    """Populate empty order_reference values with INDY-{id} format."""
    # Django
    from django.db import connection

    vendor = connection.vendor

    with connection.cursor() as cursor:
        if vendor == "mysql":
            # Ensure columns exist (handles environments where 0049 state advanced
            # but physical columns were not created due to cross-schema lookup).
            cursor.execute(
                """
                SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_NAME = 'indy_hub_materialexchangesellorder'
                AND TABLE_SCHEMA = DATABASE()
                AND COLUMN_NAME = 'order_reference'
            """
            )
            if not cursor.fetchone():
                cursor.execute(
                    """
                    ALTER TABLE indy_hub_materialexchangesellorder
                    ADD COLUMN order_reference VARCHAR(50) DEFAULT '' NOT NULL
                """
                )

            cursor.execute(
                """
                SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_NAME = 'indy_hub_materialexchangebuyorder'
                AND TABLE_SCHEMA = DATABASE()
                AND COLUMN_NAME = 'order_reference'
            """
            )
            if not cursor.fetchone():
                cursor.execute(
                    """
                    ALTER TABLE indy_hub_materialexchangebuyorder
                    ADD COLUMN order_reference VARCHAR(50) DEFAULT '' NOT NULL
                """
                )

            # MySQL uses CONCAT
            cursor.execute(
                """
                UPDATE indy_hub_materialexchangesellorder
                SET order_reference = CONCAT('INDY-', id)
                WHERE order_reference = '' OR order_reference IS NULL
            """
            )

            cursor.execute(
                """
                UPDATE indy_hub_materialexchangebuyorder
                SET order_reference = CONCAT('INDY-', id)
                WHERE order_reference = '' OR order_reference IS NULL
            """
            )
        elif vendor == "sqlite":
            # SQLite uses || for string concatenation
            cursor.execute(
                """
                UPDATE indy_hub_materialexchangesellorder
                SET order_reference = 'INDY-' || id
                WHERE order_reference = '' OR order_reference IS NULL
            """
            )

            cursor.execute(
                """
                UPDATE indy_hub_materialexchangebuyorder
                SET order_reference = 'INDY-' || id
                WHERE order_reference = '' OR order_reference IS NULL
            """
            )


def reverse_populate_order_references(apps, schema_editor):
    """No-op for reverse."""
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("indy_hub", "0049_add_order_reference"),
    ]

    operations = [
        # Populate empty references before the unique constraint is enforced
        migrations.RunPython(
            populate_order_references,
            reverse_populate_order_references,
        ),
    ]
