# Generated by Django 3.2.15 on 2022-11-07 13:48

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

import academic_community.channels.fields
import academic_community.channels.models.subscriptions
import academic_community.ckeditor5.fields
from academic_community.channels.emojies import emojies


def create_view_availability_permission(apps, schema_editor):
    """Create the subscription permission for the user model."""
    User = apps.get_model("auth", "User")
    Permission = apps.get_model("auth", "Permission")
    ContentType = apps.get_model("contenttypes", "ContentType")
    Group = apps.get_model("auth", "Group")
    ChatSettings = apps.get_model("chats", "ChatSettings")
    GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission")
    UserObjectPermission = apps.get_model("guardian", "UserObjectPermission")

    user_ct = ContentType.objects.get_for_model(User)

    permission = Permission.objects.get_or_create(
        codename="view_availability",
        name="Can view availability",
        content_type=user_ct,
    )[0]

    for user in User.objects.all():
        members_group = Group.objects.get_or_create(name="Community members")[
            0
        ]
        settings = ChatSettings.objects.get(user=user)
        settings.group_view_availability_permission.add(members_group)
        GroupObjectPermission.objects.create(
            permission=permission,
            content_type=user_ct,
            object_pk=user.pk,
            group=members_group,
        )
        UserObjectPermission.objects.create(
            permission=permission,
            content_type=user_ct,
            object_pk=user.pk,
            user=user,
        )


def create_default_profile_button_classes(apps, schema_editor):
    """Create the default profile button classes"""
    ProfileButtonClass = apps.get_model("chats", "ProfileButtonClass")

    default_profiles = {
        "Black": "black",
        "Dim Grey": "dim-grey",
        "Grey": "grey",
        "Light Grey": "light-grey",
        "Red": "red",
        "Orange": "orange",
        "Yellow": "yellow",
        "Light Green": "light-green",
        "Green": "green",
        "Aquamarine": "aquamarine",
        "Turquoise": "turquoise",
        "Light Blue": "light-blue",
        "Blue": "blue",
        "Purple": "purple",
    }

    buttons = []

    for name, profile in default_profiles.items():
        buttons.append(
            ProfileButtonClass.objects.create(
                name=name, classes="btn-profile-" + profile
            )
        )


class Migration(migrations.Migration):

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("chats", "0003_channelsubscription_unread"),
    ]

    operations = [
        migrations.AddField(
            model_name="chatsettings",
            name="availability",
            field=models.CharField(
                blank=True,
                choices=[
                    ("ONLINE", "🟢 Online"),
                    ("AWAY", "🟡 Away"),
                    ("DONOTDISTURB", "🔴 Do Not Disturb"),
                    ("OFFLINE", "⚪ Offline"),
                    ("DISABLED", " Disabled"),
                ],
                help_text="Your availability",
                max_length=20,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="chatsettings",
            name="availability_reset_date",
            field=models.DateTimeField(
                blank=True,
                help_text="Specify a time when your availability should be switched to the reset value",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="chatsettings",
            name="availability_reset_value",
            field=models.CharField(
                blank=True,
                choices=[
                    ("ONLINE", "🟢 Online"),
                    ("AWAY", "🟡 Away"),
                    ("DONOTDISTURB", "🔴 Do Not Disturb"),
                    ("OFFLINE", "⚪ Offline"),
                    ("DISABLED", " Disabled"),
                ],
                help_text="Specify your availability mode that you want to switch to at the availability reset date (or leave it empty to switch to let the website update your availability automatically).",
                max_length=20,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="chatsettings",
            name="group_view_availability_permission",
            field=models.ManyToManyField(
                blank=True,
                related_name="view_availability_permission",
                to="auth.Group",
                verbose_name="Groups whose users can view my availability",
            ),
        ),
        migrations.AddField(
            model_name="chatsettings",
            name="user_view_availability_permission",
            field=models.ManyToManyField(
                blank=True,
                related_name="view_availability_permission",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Users that can view my availability",
            ),
        ),
        migrations.AlterField(
            model_name="comment",
            name="body",
            field=academic_community.ckeditor5.fields.CKEditor5Field(
                help_text="The content of the comment."
            ),
        ),
        migrations.RunPython(
            create_view_availability_permission, migrations.RunPython.noop
        ),
        migrations.CreateModel(
            name="CommentReaction",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "emoji",
                    academic_community.channels.fields.EmojiField(
                        choices=emojies,
                        help_text="The emoji of the reaction.",
                        max_length=78,
                    ),
                ),
                (
                    "comment",
                    models.ForeignKey(
                        help_text="The comment this reaction refers to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.comment",
                    ),
                ),
                (
                    "users",
                    models.ManyToManyField(
                        help_text="The users that reacted with this emoji.",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="commentreaction",
            constraint=models.UniqueConstraint(
                fields=("emoji", "comment"), name="unique_emoji_for_comment"
            ),
        ),
        migrations.AddField(
            model_name="thread",
            name="expand_for_all",
            field=models.BooleanField(
                default=False,
                help_text="Expand the comments in this thread for everyone.",
            ),
        ),
        migrations.AddField(
            model_name="thread",
            name="user_expanded",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users for which the comments in this thread are expanded by default.",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.CreateModel(
            name="ProfileButtonClass",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the profile button class",
                        max_length=50,
                    ),
                ),
                (
                    "classes",
                    models.CharField(
                        help_text="Classes for the profile button",
                        max_length=50,
                    ),
                ),
            ],
        ),
        migrations.RunPython(
            create_default_profile_button_classes, migrations.RunPython.noop
        ),
        migrations.AddField(
            model_name="chatsettings",
            name="profile_button_class",
            field=models.ForeignKey(
                default=academic_community.channels.models.subscriptions.random_profile_button_class,
                help_text="Class for the profile button",
                on_delete=django.db.models.deletion.SET_DEFAULT,
                to="chats.profilebuttonclass",
            ),
        ),
    ]
