# Generated by Django 5.2.8 on 2025-11-23 00:16

from django.db import migrations


def seed_dashboard_rules(apps, schema_editor):
    ContentType = apps.get_model("contenttypes", "ContentType")
    DashboardRule = apps.get_model("pages", "DashboardRule")

    rule_definitions = [
        (
            "Charger Heartbeat",
            ("ocpp", "charger"),
            "python",
            "evaluate_evcs_heartbeat_rules",
        ),
        (
            "Charge Point Configuration",
            ("ocpp", "chargerconfiguration"),
            "python",
            "evaluate_cp_configuration_rules",
        ),
        (
            "Charge Point Firmware",
            ("ocpp", "cpfirmware"),
            "python",
            "evaluate_cp_firmware_rules",
        ),
        (
            "Node Health",
            ("nodes", "node"),
            "python",
            "evaluate_node_rules",
        ),
    ]

    for name, ct_key, implementation, handler in rule_definitions:
        app_label, model_name = ct_key
        model = apps.get_model(app_label, model_name)
        content_type = ContentType.objects.get_for_model(model)
        DashboardRule.objects.update_or_create(
            name=name,
            defaults={
                "content_type": content_type,
                "implementation": implementation,
                "function_name": handler,
                "is_seed_data": True,
            },
        )


def unseed_dashboard_rules(apps, schema_editor):
    DashboardRule = apps.get_model("pages", "DashboardRule")
    DashboardRule.all_objects.filter(
        name__in={
            "Charger Heartbeat",
            "Charge Point Configuration",
            "Charge Point Firmware",
            "Node Health",
        }
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("pages", "0041_dashboardrule"),
        ("ocpp", "0063_charger_preferred_ocpp_version_and_more"),
        ("nodes", "0047_remove_roleconfigurationprofile_role_and_more"),
    ]

    operations = [
        migrations.RunPython(seed_dashboard_rules, unseed_dashboard_rules),
    ]
