# Generated by Django 3.2.15 on 2022-09-28 15:13

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


def create_activity_channel_groups(apps, schema_editor):
    CommunityMember = apps.get_model("members", "CommunityMember")
    ActivityChannelGroup = apps.get_model("activities", "ActivityChannelGroup")
    for member in CommunityMember.objects.filter(user__isnull=False):
        for activity in member.activities.all():
            ActivityChannelGroup.objects.create(
                activity=activity,
                user=member.user,
                name=f"{activity.category.abbreviation} {activity.abbreviation}",
                slug=activity.abbreviation,
            )


def create_links(apps, schema_editor):
    """Create links to the existing models."""
    Activity = apps.get_model("activities", "Activity")

    ActivityMentionLink = apps.get_model("activities", "ActivityMentionLink")

    for activity in Activity.objects.all():
        ActivityMentionLink.objects.create(related_model=activity)


class Migration(migrations.Migration):

    dependencies = [
        ("chats", "0001_initial"),
        ("activities", "0014_auto_20220919_1955"),
    ]

    operations = [
        migrations.CreateModel(
            name="ActivityChannelRelation",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "activity",
                    models.ForeignKey(
                        help_text="The related group in the community that this channel belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="activities.activity",
                        verbose_name="Working Group/Project Group",
                    ),
                ),
                (
                    "channel",
                    models.ForeignKey(
                        help_text="The channel that this relation corresponds to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
                (
                    "is_default",
                    models.BooleanField(
                        default=False,
                        help_text="Is this relation the default entry point for accessing the channel?",
                    ),
                ),
                (
                    "symbolic_relation",
                    models.BooleanField(
                        default=False,
                        help_text="If this is a symbolic relation, no members are given access to this channel unless they are explicitly added.",
                    ),
                ),
                (
                    "subscribe_members",
                    models.BooleanField(
                        default=True,
                        help_text="If enabled, all current and future activity members are subscribed to the channel.",
                        verbose_name="Automatically subscribe members to the channel",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="ActivityChannelGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
                (
                    "activity",
                    models.ForeignKey(
                        help_text="Select a working/project group whose channels you want to display.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="activities.activity",
                    ),
                ),
            ],
            bases=("chats.channelgroup",),
        ),
        migrations.RunPython(
            create_activity_channel_groups, migrations.RunPython.noop
        ),
        migrations.CreateModel(
            name="ActivityMentionLink",
            fields=[
                (
                    "mentionlink_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.mentionlink",
                    ),
                ),
                (
                    "related_model",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="activities.activity",
                    ),
                ),
            ],
            bases=("chats.mentionlink",),
        ),
        migrations.RunPython(create_links, migrations.RunPython.noop),
        migrations.AlterModelOptions(
            name="activity",
            options={
                "ordering": [
                    django.db.models.expressions.OrderBy(
                        django.db.models.expressions.F("end_date"),
                        nulls_first=True,
                    )
                ],
                "permissions": (
                    ("change_activity_lead", "Can change activity leader"),
                    (
                        "add_material_relation",
                        "Can add an activity material relation",
                    ),
                    (
                        "add_channel_relation",
                        "Can add an activity channel relation",
                    ),
                ),
                "verbose_name": "Working/Project Group",
                "verbose_name_plural": "Working/Project Groups",
            },
        ),
        migrations.AddField(
            model_name="activity",
            name="group_add_channel_relation_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups that can create channels and relate them to this activity.",
                related_name="activity_add_channel_relation",
                to="auth.Group",
                verbose_name="Groups with channel creation permission",
            ),
        ),
        migrations.AddField(
            model_name="activity",
            name="group_add_material_relation_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Groups that can upload and relate material to this activity.",
                related_name="activity_add_material_relation",
                to="auth.Group",
                verbose_name="Groups with material upload permission",
            ),
        ),
        migrations.AddField(
            model_name="activity",
            name="user_add_channel_relation_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users that can create channels and relate them to this activity.",
                related_name="activity_add_channel_relation",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Users with channel creation permission",
            ),
        ),
        migrations.AddField(
            model_name="activity",
            name="user_add_material_relation_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users that can upload and relate material to this activity.",
                related_name="activity_add_material_relation",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Users with material upload permission",
            ),
        ),
    ]
