# Generated by Django 5.2.8 on 2025-11-22 19:07

from django.db import migrations


def set_default_cp2(apps, schema_editor):
    Simulator = apps.get_model("ocpp", "Simulator")

    cp2 = (
        Simulator.objects.filter(host="127.0.0.1", cp_path__iexact="CP2")
        .order_by("pk")
        .first()
    )
    if cp2 is None:
        return

    Simulator.objects.filter(default=True, is_deleted=False).exclude(pk=cp2.pk).update(
        default=False
    )
    cp2.default = True
    cp2.save(update_fields=["default"])


def unset_default_cp2(apps, schema_editor):
    Simulator = apps.get_model("ocpp", "Simulator")

    cp2 = Simulator.objects.filter(host="127.0.0.1", cp_path__iexact="CP2").first()
    if cp2:
        cp2.default = False
        cp2.save(update_fields=["default"])


class Migration(migrations.Migration):

    dependencies = [
        ("ocpp", "0060_stationmodel_charger_station_model"),
    ]

    operations = [migrations.RunPython(set_default_cp2, unset_default_cp2)]
