# Generated by Django 4.2.7 on 2023-12-04 10:41

import uuid

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


class Migration(migrations.Migration):
    replaces = [
        ("slack", "0001_initial_squashed_0022_alter_sos_user_group"),
        ("slack", "0023_delete_incidentchannel_incidentchannel"),
    ]

    dependencies = [
        ("incidents", "0026_alter_user_options"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        (
            "incidents",
            "0013_environment_value_squashed_0017_rename_slack_groups_component_slack_conversations",
        ),
        ("incidents", "0023_auto_20220622_1559"),
        ("incidents", "0022_alter_user_name"),
        ("incidents", "0011_auto_20210628_1427"),
        ("incidents", "0027_remove_component_slack_conversations"),
        ("incidents", "0014_auto_20210809_1052"),
    ]

    operations = [
        migrations.CreateModel(
            name="Conversation",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(blank=True, max_length=80)),
                ("channel_id", models.CharField(max_length=128)),
                (
                    "_status",
                    models.IntegerField(
                        choices=[(0, "Not Opened"), (1, "Opened"), (2, "Archived")],
                        db_column="status",
                        default=0,
                    ),
                ),
                (
                    "_type",
                    models.IntegerField(
                        choices=[
                            (0, "Unknown"),
                            (10, "Public Channel"),
                            (20, "Private Channel"),
                            (30, "DM"),
                            (40, "Group DM"),
                        ],
                        db_column="type",
                        default=0,
                    ),
                ),
                ("is_shared", models.BooleanField(default=False)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "tag",
                    models.CharField(
                        blank=True,
                        help_text="Used by FireFighter internally to mark special conversations (#it-deploy, #tech-incidents...). Must be empty or unique.",
                        max_length=80,
                    ),
                ),
                (
                    "components",
                    models.ManyToManyField(
                        blank=True,
                        related_name="conversations",
                        to="incidents.component",
                    ),
                ),
                (
                    "members",
                    models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL),
                ),
            ],
        ),
        migrations.CreateModel(
            name="IncidentChannel",
            fields=[
                (
                    "conversation_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="slack.conversation",
                    ),
                ),
            ],
            bases=("slack.conversation",),
        ),
        migrations.CreateModel(
            name="UserGroup",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True,
                        help_text="Friendly name of the group. Corresponds to the `name` field in the Slack API. Max 80 characters.",
                        max_length=80,
                    ),
                ),
                (
                    "handle",
                    models.CharField(
                        blank=True,
                        help_text="Indicates the value used to notify group members via a mention. The handle does not include the leading @. Corresponds to the `handle` field in the Slack API. Max 80 characters.",
                        max_length=80,
                    ),
                ),
                (
                    "description",
                    models.CharField(
                        blank=True,
                        help_text="A short description of the group. Corresponds to the `description` field in the Slack API. Max 140 characters.",
                        max_length=140,
                    ),
                ),
                (
                    "usergroup_id",
                    models.CharField(
                        blank=True,
                        help_text="The Slack usergroup ID of the usergroup. Usually starts with `S`.",
                        max_length=32,
                        null=True,
                        unique=True,
                    ),
                ),
                (
                    "is_external",
                    models.BooleanField(
                        default=False,
                        help_text="Is this an external group, from an external Slack Workspace? Corresponds to the `is_external` field in the Slack API.",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "components",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Incident created with this usergroup automatically add the group members to these components.",
                        related_name="usergroups",
                        to="incidents.component",
                    ),
                ),
                (
                    "members",
                    models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL),
                ),
            ],
            options={
                "verbose_name": "Slack user group",
                "verbose_name_plural": "Slack user groups",
            },
        ),
        migrations.CreateModel(
            name="Sos",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                (
                    "conversation",
                    models.ForeignKey(
                        blank=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="slack.conversation",
                    ),
                ),
                (
                    "user_group",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="slack.usergroup",
                    ),
                ),
            ],
            options={
                "verbose_name": "SOS",
                "verbose_name_plural": "SOS",
            },
        ),
        migrations.CreateModel(
            name="SlackUser",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("slack_id", models.CharField(max_length=32, unique=True)),
                (
                    "username",
                    models.CharField(
                        blank=True,
                        help_text="The Slack handle of a Slack user (@handle)",
                        max_length=32,
                        null=True,
                    ),
                ),
                ("image", models.URLField(blank=True, null=True)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="slack_user",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Message",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("ts", models.DateTimeField(help_text="UTC Timestamp of the message")),
                ("thread_ts", models.DateTimeField(blank=True, null=True)),
                ("type", models.CharField(max_length=80)),
                ("subtype", models.CharField(blank=True, max_length=80)),
                ("text", models.TextField(blank=True, max_length=40000)),
                ("blocks", models.JSONField(blank=True, null=True)),
                ("ff_type", models.CharField(blank=True, max_length=64)),
                (
                    "conversation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="slack.conversation",
                    ),
                ),
                (
                    "incident",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="slack_message",
                        to="incidents.incident",
                    ),
                ),
                (
                    "incident_update",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="slack_message",
                        to="incidents.incidentupdate",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="slack.slackuser",
                    ),
                ),
            ],
        ),
        migrations.AddIndex(
            model_name="slackuser",
            index=models.Index(
                fields=["slack_id"], name="slack_slack_slack_i_06073f_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="message",
            constraint=models.UniqueConstraint(
                fields=("ts", "conversation"), name="message_ts_unicity"
            ),
        ),
        migrations.AddField(
            model_name="incidentchannel",
            name="incident",
            field=models.OneToOneField(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="conversation",
                to="incidents.incident",
            ),
        ),
        migrations.AddConstraint(
            model_name="conversation",
            constraint=models.CheckConstraint(
                check=models.Q(("_type__in", [0, 10, 20, 30, 40])),
                name="slack_conversation__type_valid",
            ),
        ),
        migrations.AddConstraint(
            model_name="conversation",
            constraint=models.CheckConstraint(
                check=models.Q(("_status__in", [0, 1, 2])),
                name="slack_conversation__status_valid",
            ),
        ),
        migrations.AddConstraint(
            model_name="conversation",
            constraint=models.UniqueConstraint(
                condition=models.Q(("tag", ""), _negated=True),
                fields=("tag",),
                name="unique__tag",
            ),
        ),
    ]
