# Generated by Django 5.2.8 on 2025-12-07 16:33

from django.db import migrations

from apps.app.models import DEFAULT_MODEL_WIKI_URLS


APP_NAME = "ocpp"
MODEL_LABEL = "ocpp.Charger"
WIKI_URL = DEFAULT_MODEL_WIKI_URLS.get((APP_NAME, MODEL_LABEL))


def _get_ocpp_application(apps):
    Application = apps.get_model("app", "Application")

    try:
        return Application.objects.get(name=APP_NAME)
    except Application.DoesNotExist:
        return None


def set_ocpp_wiki_url(apps, schema_editor):
    ApplicationModel = apps.get_model("app", "ApplicationModel")
    application = _get_ocpp_application(apps)
    if not application or not WIKI_URL:
        return

    ApplicationModel.objects.filter(
        application=application, label__iexact=MODEL_LABEL
    ).update(wiki_url=WIKI_URL)


def unset_ocpp_wiki_url(apps, schema_editor):
    ApplicationModel = apps.get_model("app", "ApplicationModel")
    application = _get_ocpp_application(apps)
    if not application or not WIKI_URL:
        return

    ApplicationModel.objects.filter(
        application=application, label__iexact=MODEL_LABEL, wiki_url=WIKI_URL
    ).update(wiki_url="")


class Migration(migrations.Migration):

    dependencies = [
        ("app", "0006_mark_cards_critical"),
    ]

    operations = [
        migrations.RunPython(set_ocpp_wiki_url, reverse_code=unset_ocpp_wiki_url),
    ]
