# Generated by Django 3.2.15 on 2022-09-23 08:45

from django.conf import settings
from django.db import migrations, models
from django.forms.models import model_to_dict


def migrate_communitymaterial(apps, schema_editor):
    """Migrate the community material to Material."""
    CommunityMaterial = apps.get_model(
        "uploaded_material", "CommunityMaterial"
    )
    for communitymaterial in CommunityMaterial.objects.all():
        material = communitymaterial.material_ptr
        material.group_view_permission.add(
            *communitymaterial.old_group_view_permission.all()
        )
        material.group_change_permission.add(
            *communitymaterial.old_group_change_permission.all()
        )
        material.user_view_permission.add(
            *communitymaterial.old_user_view_permission.all()
        )
        material.user_change_permission.add(
            *communitymaterial.old_user_change_permission.all()
        )


def undo_migrate_communitymaterial(apps, schema_editor):
    """Undo the migration of community material to material"""
    CommunityMaterial = apps.get_model(
        "uploaded_material", "CommunityMaterial"
    )
    Material = apps.get_model("uploaded_material", "Material")
    exclude = [
        "keywords",
        "group_view_permission",
        "group_change_permission",
        "user_view_permission",
        "user_change_permission",
    ]
    for material in Material.objects.all():
        kws = model_to_dict(material, exclude=exclude)
        kws["user"] = material.user
        kws["category"] = material.category
        kws["license"] = material.license
        kws["date_created"] = material.date_created
        communitymaterial = CommunityMaterial.objects.create(
            material_ptr=material, **kws
        )
        communitymaterial.old_group_view_permission.add(
            *material.group_view_permission.all()
        )
        communitymaterial.old_group_change_permission.add(
            *material.group_change_permission.all()
        )
        communitymaterial.old_user_view_permission.add(
            *material.user_view_permission.all()
        )
        communitymaterial.old_user_change_permission.add(
            *material.user_change_permission.all()
        )


class Migration(migrations.Migration):

    dependencies = [
        ("programme", "0023_auto_20220922_2028"),
        ("topics", "0011_auto_20220922_1955"),
        ("auth", "0012_alter_user_first_name_max_length"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("chats", "0001_initial"),
        ("activities", "0014_auto_20220919_1955"),
        ("uploaded_material", "0009_alter_material_uuid"),
    ]

    operations = [
        migrations.RenameField(
            model_name="communitymaterial",
            old_name="group_change_permission",
            new_name="old_group_change_permission",
        ),
        migrations.RenameField(
            model_name="communitymaterial",
            old_name="group_view_permission",
            new_name="old_group_view_permission",
        ),
        migrations.RenameField(
            model_name="communitymaterial",
            old_name="user_change_permission",
            new_name="old_user_change_permission",
        ),
        migrations.RenameField(
            model_name="communitymaterial",
            old_name="user_view_permission",
            new_name="old_user_view_permission",
        ),
        migrations.AddField(
            model_name="material",
            name="group_change_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups with write permission (this does not include read permission!)",
                related_name="material_write",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="group_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups with read permission",
                related_name="material_read",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="user_change_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit write permission (this does not include read permission!)",
                related_name="material_write",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="user_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit read permission",
                related_name="material_read",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AlterField(
            model_name="addcommunitymaterialbuttonpluginmodel",
            name="group_change_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups with write permission (this does not include read permission!)",
                related_name="addmaterialpluginmodel_write",
                to="auth.Group",
            ),
        ),
        migrations.AlterField(
            model_name="addcommunitymaterialbuttonpluginmodel",
            name="group_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups with read permission",
                related_name="addmaterialpluginmodel_read",
                to="auth.Group",
            ),
        ),
        migrations.AlterField(
            model_name="addcommunitymaterialbuttonpluginmodel",
            name="user_change_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit write permission (this does not include read permission!)",
                related_name="addmaterialpluginmodel_write",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AlterField(
            model_name="addcommunitymaterialbuttonpluginmodel",
            name="user_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit read permission",
                related_name="addmaterialpluginmodel_read",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.RunPython(
            migrate_communitymaterial, undo_migrate_communitymaterial
        ),
        migrations.DeleteModel(
            name="CommunityMaterial",
        ),
    ]
