# Generated by Django 3.2.15 on 2022-08-24 12:37

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


def migrate_groups(apps, schema_editor):
    """Migrate existing activity groups"""
    ActivityGroup = apps.get_model("activities", "ActivityGroup")
    Activity = apps.get_model("activities", "Activity")
    User = apps.get_model("auth", "User")

    for activity in Activity.objects.all():
        if activity.abbreviation and activity.name != activity.abbreviation:
            name = f"{activity.name} ({activity.abbreviation})"
        else:
            name = activity.name
        if activity.category and activity.category.abbreviation:
            name = f"{activity.category.abbreviation} {name}"
        group = activity.groups.first()
        if group:
            ActivityGroup.objects.create(
                name=f"Members of {name}",
                activity=activity,
                group_ptr=group,
            )
        else:
            group = ActivityGroup.objects.create(
                name=f"Members of {name}",
                activity=activity,
            )
            for members in [
                activity.communitymember_set.all(),
                activity.former_members.all(),
            ]:
                group.user_set.add(
                    *User.objects.filter(
                        pk__in=members.values_list("user__pk", flat=True)
                    )
                )


def unmigrate_groups(apps, schema_editor):
    """Migrate existing activity groups"""
    ActivityGroup = apps.get_model("activities", "ActivityGroup")

    for group in ActivityGroup.objects.all():
        group.activity.groups.add(group.group_ptr)
        group.delete(keep_parents=True)


class Migration(migrations.Migration):

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        ("activities", "0011_activity_leaders"),
    ]

    operations = [
        migrations.CreateModel(
            name="ActivityGroup",
            fields=[
                (
                    "group_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="auth.group",
                    ),
                ),
                (
                    "activity",
                    models.OneToOneField(
                        help_text="The associated activity for the group",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="activities.activity",
                    ),
                ),
            ],
            bases=("auth.group",),
            managers=[
                ("objects", django.contrib.auth.models.GroupManager()),
            ],
        ),
        migrations.RunPython(migrate_groups, unmigrate_groups),
        migrations.RemoveField(
            model_name="activity",
            name="groups",
        ),
    ]
