# Generated by Django 4.2.3 on 2023-10-13 12:40
from django.db import migrations
from guardian.shortcuts import assign_perm

def create_group(apps, project, field_name, permissions):
    Group = apps.get_model("auth", "Group")
    Permission = apps.get_model("auth", "Permission")
    GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission")
    ContentType = apps.get_model("contenttypes", "ContentType")
    group = Group.objects.create(name=f'LDP_{project._meta.model_name}_{field_name}_{project.id}')
    setattr(project, field_name, group)
    for perm_name in permissions:
        ctype = ContentType.objects.get(model='project')
        permission = Permission.objects.get(content_type=ctype, codename=f'{perm_name}_project')
        GroupObjectPermission.objects.get_or_create(permission=permission, group=group, content_type=ctype, object_pk=project.pk)

def migrate_groups(apps, schema_editor):
    '''copies the members and admins of the projects into new groups'''
    Project = apps.get_model("djangoldp_project", "Project")
    for project in Project.objects.all():
        create_group(apps, project, 'members', ['view'])
        create_group(apps, project, 'admins', ['view', 'change', 'control'])

        for member in project.oldmembers.all():
            project.members.user_set.add(member.user)
            if member.is_admin:
                project.admins.user_set.add(member.user)
        project.save()

def reverse_migrate_groups(apps, schema_editor):
    '''copies the members of the projects into the old member model'''
    Project = apps.get_model("djangoldp_project", "Project")
    Member = apps.get_model("djangoldp_project", "Member")
    for project in Project.objects.all():
        if project.members:
            for user in project.members.user_set.all():
                if not Member.objects.filter(user=user, project=project).exists():
                    is_admin = user in project.admins.user_set.all()
                    Member.objects.create(user=user, project=project, is_admin=is_admin)
            project.members.delete()
        if project.admins:
            project.admins.delete()


def migrate_status(apps, schema_editor):
    '''copies the status of the projects into the public flag'''
    Project = apps.get_model("djangoldp_project", "Project")
    for project in Project.objects.all():
        if project.status == 'Public':
            project.public = True
            project.save()

def reverse_migrate_status(apps, schema_editor):
    '''copies the status of the projects into the public flag'''
    Project = apps.get_model("djangoldp_project", "Project")
    for project in Project.objects.all():
        project.status == 'Public' if project.public else 'Private'


def migrate_communities(apps, schema_editor):
    '''copies the communities from the intermediary model into the inner field'''
    CommunityProject = apps.get_model("djangoldp_community", "CommunityProject")
    for communityproject in CommunityProject.objects.all():
        communityproject.project.community = communityproject.community
        communityproject.project.save()

def reverse_migrate_communities(apps, schema_editor):
    '''copies the communities from the inner field into the intermediary model'''
    Project = apps.get_model("djangoldp_project", "Project")
    CommunityProject = apps.get_model("djangoldp_community", "CommunityProject")
    for project in Project.objects.all():
        if project.community:
            project.oldcommunity, created = CommunityProject.objects.get_or_create(project=project, community=project.community)
            project.save()


class Migration(migrations.Migration):
    dependencies = [
        ('djangoldp_project', '0023_alter_businessprovider_options_and_more'),
    ]

    operations = [
        migrations.RunPython(migrate_status, reverse_migrate_status),
        migrations.RunPython(migrate_groups, reverse_migrate_groups),
        migrations.RunPython(migrate_communities, reverse_migrate_communities),
    ]