# Generated by Django 3.2.15 on 2023-01-27 08:35

from django.db import migrations, models


def set_sessionmaterial_permissions(apps, schema_editor):
    SessionMaterialRelation = apps.get_model(
        "programme", "SessionMaterialRelation"
    )
    Session = apps.get_model("programme", "Session")
    ContentType = apps.get_model("contenttypes", "ContentType")
    UserObjectPermission = apps.get_model("guardian", "UserObjectPermission")
    GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission")
    Permission = apps.get_model("auth", "Permission")
    Material = apps.get_model("uploaded_material", "Material")

    material_ct = ContentType.objects.get_for_model(Material)
    session_ct = ContentType.objects.get_for_model(Session)

    permissions = []
    for codename in [
        "view_material",
        "change_material",
        "delete_material",
    ]:
        try:
            permissions.append(
                Permission.objects.get(
                    codename=codename, content_type=material_ct
                )
            )
        except Permission.DoesNotExist:
            pass

    for relation in SessionMaterialRelation.objects.filter(
        symbolic_relation=False
    ):
        relation.is_default = True
        relation.save()
        groups = [
            relation.session.event.registration_group,
        ]
        if not relation.registered_only:
            groups.extend(relation.session.event.view_programme_groups.all())
        orga_group = relation.session.event.orga_group
        for permission in permissions:
            GroupObjectPermission.objects.get_or_create(
                permission=permission,
                content_type=material_ct,
                object_pk=relation.material.pk,
                group=orga_group,
            )
        for group in groups:
            GroupObjectPermission.objects.get_or_create(
                permission=permissions[0],
                content_type=material_ct,
                object_pk=relation.material.pk,
                group=group,
            )
        for perm_map in UserObjectPermission.objects.filter(
            object_pk=relation.session.pk,
            content_type=session_ct,
            permission__codename="change_session",
            permission__content_type=session_ct,
        ):
            for permission in permissions:
                UserObjectPermission.objects.get_or_create(
                    permission=permission,
                    content_type=material_ct,
                    object_pk=relation.material.pk,
                    user=perm_map.user,
                )


def set_contributionmaterial_permissions(apps, schema_editor):
    ContributionMaterialRelation = apps.get_model(
        "programme", "ContributionMaterialRelation"
    )
    Contribution = apps.get_model("programme", "Contribution")
    ContentType = apps.get_model("contenttypes", "ContentType")
    UserObjectPermission = apps.get_model("guardian", "UserObjectPermission")
    Permission = apps.get_model("auth", "Permission")
    Material = apps.get_model("uploaded_material", "Material")

    material_ct = ContentType.objects.get_for_model(Material)
    contribution_ct = ContentType.objects.get_for_model(Contribution)

    permissions = []
    for codename in [
        "view_material",
        "change_material",
        "delete_material",
    ]:
        try:
            permissions.append(
                Permission.objects.get(
                    codename=codename, content_type=material_ct
                )
            )
        except Permission.DoesNotExist:
            pass

    contrib_perms = ["change_contribution", "upload_material"]

    for relation in ContributionMaterialRelation.objects.filter(
        symbolic_relation=False
    ):
        relation.is_default = True
        relation.save()
        for contrib_perm in contrib_perms:
            for perm_map in UserObjectPermission.objects.filter(
                object_pk=relation.contribution.pk,
                content_type=contribution_ct,
                permission__codename=contrib_perm,
                permission__content_type=contribution_ct,
            ):
                for permission in permissions:
                    UserObjectPermission.objects.get_or_create(
                        permission=permission,
                        content_type=material_ct,
                        object_pk=relation.material.pk,
                        user=perm_map.user,
                    )


class Migration(migrations.Migration):

    dependencies = [
        (
            "programme",
            "0024_sessionmaterialrelation_unique_session_relation_for_material",
        ),
    ]

    operations = [
        migrations.AddField(
            model_name="sessionmaterialrelation",
            name="is_default",
            field=models.BooleanField(
                default=False,
                help_text="Is this relation the default entry point?",
            ),
        ),
        migrations.AddField(
            model_name="sessionmaterialrelation",
            name="symbolic_relation",
            field=models.BooleanField(
                default=False,
                help_text="If this is a symbolic relation, this relation does not have any influences on the permissions.",
            ),
        ),
        migrations.RunPython(
            set_sessionmaterial_permissions, migrations.RunPython.noop
        ),
        migrations.RunPython(
            set_contributionmaterial_permissions, migrations.RunPython.noop
        ),
    ]
