# Generated by Django 5.2.4 on 2025-09-13 04:04

from django.db import migrations, models


def link_modules_to_applications(apps, schema_editor):
    Module = apps.get_model("pages", "Module")
    Application = apps.get_model("pages", "Application")
    for module in Module.objects.all():
        # Ensure the module's application exists
        if not Application.objects.filter(pk=module.application_id).exists():
            # Try to infer application from the first segment of the path
            slug = module.path.strip("/").split("/")[0]
            app = Application.objects.filter(name=slug).first()
            if app:
                module.application = app
                module.save(update_fields=["application"])
            else:
                module.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("pages", "0002_experiencereference"),
    ]

    operations = [
        migrations.AddField(
            model_name="application",
            name="is_user_data",
            field=models.BooleanField(default=False, editable=False),
        ),
        migrations.AddField(
            model_name="favorite",
            name="is_user_data",
            field=models.BooleanField(default=False, editable=False),
        ),
        migrations.AddField(
            model_name="landing",
            name="is_user_data",
            field=models.BooleanField(default=False, editable=False),
        ),
        migrations.AddField(
            model_name="module",
            name="is_user_data",
            field=models.BooleanField(default=False, editable=False),
        ),
        migrations.AddField(
            model_name="sitebadge",
            name="is_user_data",
            field=models.BooleanField(default=False, editable=False),
        ),
        migrations.RunPython(link_modules_to_applications, migrations.RunPython.noop),
    ]
