# Generated by Django 5.2.4 on 2025-09-18 17:19

from django.db import migrations, models


def normalize_connector_id_strings(apps, schema_editor):
    """Ensure connector ids are numeric strings or null before cast."""

    Charger = apps.get_model("ocpp", "Charger")

    for charger in Charger.objects.all():
        value = charger.connector_id
        if value is None:
            continue
        value = str(value).strip()
        if not value:
            charger.connector_id = None
            charger.save(update_fields=["connector_id"])
            continue
        try:
            normalized = int(value)
        except (TypeError, ValueError):
            charger.connector_id = None
        else:
            charger.connector_id = str(normalized)
        charger.save(update_fields=["connector_id"])


class Migration(migrations.Migration):

    dependencies = [
        ("ocpp", "0007_charger_display_name"),
    ]

    operations = [
        migrations.RunPython(normalize_connector_id_strings, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="charger",
            name="connector_id",
            field=models.PositiveIntegerField(
                blank=True,
                help_text="Optional connector identifier for multi-connector chargers.",
                null=True,
                verbose_name="Connector ID",
            ),
        ),
        migrations.AlterField(
            model_name="metervalue",
            name="connector_id",
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="simulator",
            name="cp_path",
            field=models.CharField(
                help_text="Charge Point WS path",
                max_length=100,
                verbose_name="Serial Number",
            ),
        ),
        migrations.AlterField(
            model_name="transaction",
            name="connector_id",
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
    ]
