# Generated by Django 3.2.18 on 2023-07-13 09:01

import django.contrib.postgres.indexes
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("core", "0071_trashentry_trash_item_owner"),
    ]

    operations = [
        migrations.CreateModel(
            name="Notification",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created_on",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="The date and time when the notification was created.",
                    ),
                ),
                (
                    "type",
                    models.CharField(
                        help_text="The type of notification.", max_length=64
                    ),
                ),
                (
                    "broadcast",
                    models.BooleanField(
                        default=False,
                        help_text="If True, then the notification will be sent to all users. A broadcast notification will not immediately create all the NotificationRecipient objects, but will do so when the notification is read or cleared by a user.",
                    ),
                ),
                (
                    "data",
                    models.JSONField(
                        default=dict, help_text="The data of the notification."
                    ),
                ),
            ],
            options={
                "ordering": ["-created_on"],
            },
        ),
        migrations.CreateModel(
            name="NotificationRecipient",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "read",
                    models.BooleanField(
                        default=False,
                        help_text="If True, then the notification has been read by the user. ",
                    ),
                ),
                (
                    "cleared",
                    models.BooleanField(
                        default=False,
                        help_text="If True, then the notification has been cleared by the user. Cleared notifications will not be visible by the user anymore.",
                    ),
                ),
                (
                    "created_on",
                    models.DateTimeField(
                        help_text="A copy of the notification created_on field needed to speed up queries."
                    ),
                ),
                (
                    "broadcast",
                    models.BooleanField(
                        default=False,
                        help_text="A copy of the notification broadcast field needed to speed up queries.",
                    ),
                ),
                (
                    "workspace_id",
                    models.BigIntegerField(
                        help_text="A copy of the notification workspace_id field needed to speed up queries.",
                        null=True,
                    ),
                ),
                (
                    "notification",
                    models.ForeignKey(
                        help_text="The notification that will be sent to the recipient.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="core.notification",
                    ),
                ),
                (
                    "recipient",
                    models.ForeignKey(
                        help_text="The user that will receive the notification.",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_on"],
            },
        ),
        migrations.AddField(
            model_name="notification",
            name="recipients",
            field=models.ManyToManyField(
                help_text="The users that will receive the notification. For broadcast notifications, the recipients will be created when the notification is read or cleared by the user.For direct notifications, the recipients will be created immediately when the notification is created.",
                related_name="notifications",
                through="core.NotificationRecipient",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="notification",
            name="sender",
            field=models.ForeignKey(
                help_text="The user that will receive the notification.",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="sent_notifications",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="notification",
            name="workspace",
            field=models.ForeignKey(
                help_text="The workspace where the notification lives.If the notification is a broadcast notification, then the workspace will be None.Workspace can be null also if the notification is not associated with a specific workspace or if the user does not have access to the workspace yet, like for a workspace invitation.",
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="notifications",
                to="core.workspace",
            ),
        ),
        migrations.AddIndex(
            model_name="notificationrecipient",
            index=models.Index(
                fields=["-created_on"], name="core_notifi_created_06e5e6_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notificationrecipient",
            index=models.Index(
                condition=models.Q(("cleared", False), ("read", False)),
                fields=["broadcast", "cleared", "read", "recipient_id", "workspace_id"],
                include=("notification_id",),
                name="unread_notif_count_idx",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="notificationrecipient",
            unique_together={("notification", "recipient")},
        ),
        migrations.AddIndex(
            model_name="notification",
            index=models.Index(
                fields=["-created_on"], name="core_notifi_created_8ed1ac_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="notification",
            index=django.contrib.postgres.indexes.GinIndex(
                fields=["data"], name="notification_data"
            ),
        ),
    ]
