# Generated by Django 5.2.8 on 2025-11-14 13:35

import django.db.models.deletion
from django.db import migrations, models


def seed_role_configuration_profiles(apps, schema_editor):
    NodeRole = apps.get_model("nodes", "NodeRole")
    RoleConfigurationProfile = apps.get_model(
        "nodes", "RoleConfigurationProfile"
    )

    defaults = {
        "Terminal": {
            "description": "Single-User Research & Development",
            "inventory_group": "terminal",
            "extra_vars": {
                "enable_celery": True,
                "requires_redis": False,
                "enable_lcd_screen": False,
                "enable_control": False,
                "nginx_mode": "internal",
            },
        },
        "Control": {
            "description": "Single-Device Testing & Special Task Appliances",
            "inventory_group": "control",
            "extra_vars": {
                "enable_celery": True,
                "requires_redis": True,
                "enable_lcd_screen": True,
                "enable_control": True,
                "nginx_mode": "internal",
            },
        },
        "Watchtower": {
            "description": "Multi-User Cloud & Orchestration",
            "inventory_group": "watchtower",
            "extra_vars": {
                "enable_celery": True,
                "requires_redis": True,
                "enable_lcd_screen": False,
                "enable_control": False,
                "nginx_mode": "public",
            },
        },
        "Satellite": {
            "description": "Multi-Device Edge, Network & Data Acquisition",
            "inventory_group": "satellite",
            "extra_vars": {
                "enable_celery": True,
                "requires_redis": True,
                "enable_lcd_screen": False,
                "enable_control": False,
                "nginx_mode": "internal",
            },
        },
    }

    for role_name, config in defaults.items():
        role, _ = NodeRole.objects.get_or_create(
            name=role_name,
            defaults={
                "description": config["description"],
                "is_seed_data": True,
            },
        )
        RoleConfigurationProfile.objects.update_or_create(
            role=role,
            defaults={
                "inventory_group": config["inventory_group"],
                "extra_vars": config["extra_vars"],
                "default_tags": [config["inventory_group"]],
                "is_seed_data": True,
            },
        )


def unseed_role_configuration_profiles(apps, schema_editor):
    RoleConfigurationProfile = apps.get_model(
        "nodes", "RoleConfigurationProfile"
    )
    RoleConfigurationProfile.objects.filter(is_seed_data=True).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("nodes", "0041_merge_20251113_2121"),
    ]

    operations = [
        migrations.CreateModel(
            name="RoleConfigurationProfile",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("is_seed_data", models.BooleanField(default=False, editable=False)),
                ("is_user_data", models.BooleanField(default=False, editable=False)),
                ("is_deleted", models.BooleanField(default=False, editable=False)),
                ("inventory_group", models.CharField(max_length=128)),
                ("extra_vars", models.JSONField(blank=True, default=dict)),
                ("default_tags", models.JSONField(blank=True, default=list)),
                (
                    "role",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="configuration_profile",
                        to="nodes.noderole",
                    ),
                ),
            ],
            options={
                "verbose_name": "Role Configuration Profile",
                "verbose_name_plural": "Role Configuration Profiles",
            },
        ),
        migrations.RunPython(
            seed_role_configuration_profiles,
            unseed_role_configuration_profiles,
        ),
    ]
