# Generated by Django 4.2.7 on 2025-12-09 21:02

import apps.chat.models
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("schedule", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Chat",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "uuid",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="UUID",
                    ),
                ),
                (
                    "chat_type",
                    models.CharField(
                        choices=[("direct", "Личный"), ("group", "Групповой")],
                        db_index=True,
                        default="direct",
                        max_length=20,
                        verbose_name="Тип чата",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True, max_length=255, verbose_name="Название"
                    ),
                ),
                ("description", models.TextField(blank=True, verbose_name="Описание")),
                (
                    "avatar",
                    models.ImageField(
                        blank=True,
                        null=True,
                        upload_to="chat/avatars/",
                        verbose_name="Аватар",
                    ),
                ),
                (
                    "messages_count",
                    models.IntegerField(default=0, verbose_name="Количество сообщений"),
                ),
                (
                    "last_message_at",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        null=True,
                        verbose_name="Последнее сообщение",
                    ),
                ),
                (
                    "is_archived",
                    models.BooleanField(
                        db_index=True, default=False, verbose_name="Архивирован"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата создания"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Дата обновления"),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_chats",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Создатель",
                    ),
                ),
                (
                    "lesson",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="chats",
                        to="schedule.lesson",
                        verbose_name="Занятие",
                    ),
                ),
            ],
            options={
                "verbose_name": "Чат",
                "verbose_name_plural": "Чаты",
                "db_table": "chats",
                "ordering": ["-last_message_at", "-created_at"],
            },
        ),
        migrations.CreateModel(
            name="Message",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "uuid",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="UUID",
                    ),
                ),
                (
                    "message_type",
                    models.CharField(
                        choices=[
                            ("text", "Текст"),
                            ("file", "Файл"),
                            ("image", "Изображение"),
                            ("video", "Видео"),
                            ("audio", "Аудио"),
                            ("system", "Системное"),
                        ],
                        default="text",
                        max_length=20,
                        verbose_name="Тип сообщения",
                    ),
                ),
                ("content", models.TextField(verbose_name="Содержимое")),
                (
                    "is_edited",
                    models.BooleanField(default=False, verbose_name="Отредактировано"),
                ),
                (
                    "edited_at",
                    models.DateTimeField(
                        blank=True, null=True, verbose_name="Дата редактирования"
                    ),
                ),
                (
                    "is_deleted",
                    models.BooleanField(
                        db_index=True, default=False, verbose_name="Удалено"
                    ),
                ),
                (
                    "deleted_at",
                    models.DateTimeField(
                        blank=True, null=True, verbose_name="Дата удаления"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="Дата отправки"
                    ),
                ),
                (
                    "chat",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="messages",
                        to="chat.chat",
                        verbose_name="Чат",
                    ),
                ),
                (
                    "reply_to",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="replies",
                        to="chat.message",
                        verbose_name="Ответ на",
                    ),
                ),
                (
                    "sender",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="sent_messages",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Отправитель",
                    ),
                ),
            ],
            options={
                "verbose_name": "Сообщение",
                "verbose_name_plural": "Сообщения",
                "db_table": "messages",
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="MessageFile",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "file",
                    models.FileField(
                        max_length=500,
                        upload_to=apps.chat.models.message_file_upload_path,
                        verbose_name="Файл",
                    ),
                ),
                (
                    "file_name",
                    models.CharField(max_length=255, verbose_name="Имя файла"),
                ),
                (
                    "file_size",
                    models.BigIntegerField(verbose_name="Размер файла (bytes)"),
                ),
                (
                    "file_type",
                    models.CharField(max_length=100, verbose_name="MIME тип"),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата загрузки"
                    ),
                ),
                (
                    "message",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="files",
                        to="chat.message",
                        verbose_name="Сообщение",
                    ),
                ),
            ],
            options={
                "verbose_name": "Файл сообщения",
                "verbose_name_plural": "Файлы сообщений",
                "db_table": "message_files",
                "ordering": ["created_at"],
            },
        ),
        migrations.CreateModel(
            name="ChatParticipant",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "role",
                    models.CharField(
                        choices=[("admin", "Администратор"), ("member", "Участник")],
                        default="member",
                        max_length=20,
                        verbose_name="Роль",
                    ),
                ),
                (
                    "unread_count",
                    models.IntegerField(default=0, verbose_name="Непрочитанных"),
                ),
                (
                    "last_read_at",
                    models.DateTimeField(
                        blank=True, null=True, verbose_name="Последнее прочтение"
                    ),
                ),
                (
                    "is_muted",
                    models.BooleanField(
                        default=False, verbose_name="Уведомления отключены"
                    ),
                ),
                (
                    "is_pinned",
                    models.BooleanField(default=False, verbose_name="Закреплен"),
                ),
                (
                    "joined_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата присоединения"
                    ),
                ),
                (
                    "left_at",
                    models.DateTimeField(
                        blank=True, null=True, verbose_name="Дата выхода"
                    ),
                ),
                (
                    "chat",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="participants",
                        to="chat.chat",
                        verbose_name="Чат",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="chat_participations",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Пользователь",
                    ),
                ),
            ],
            options={
                "verbose_name": "Участник чата",
                "verbose_name_plural": "Участники чата",
                "db_table": "chat_participants",
                "ordering": ["-joined_at"],
            },
        ),
        migrations.CreateModel(
            name="MessageRead",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "read_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="Дата прочтения"
                    ),
                ),
                (
                    "message",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="reads",
                        to="chat.message",
                        verbose_name="Сообщение",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="message_reads",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Пользователь",
                    ),
                ),
            ],
            options={
                "verbose_name": "Прочтение сообщения",
                "verbose_name_plural": "Прочтения сообщений",
                "db_table": "message_reads",
                "ordering": ["-read_at"],
                "indexes": [
                    models.Index(
                        fields=["message", "user"],
                        name="message_rea_message_19da3c_idx",
                    ),
                    models.Index(
                        fields=["user", "read_at"],
                        name="message_rea_user_id_8d07ab_idx",
                    ),
                ],
                "unique_together": {("message", "user")},
            },
        ),
        migrations.CreateModel(
            name="MessageReaction",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("emoji", models.CharField(max_length=10, verbose_name="Эмодзи")),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="Дата"
                    ),
                ),
                (
                    "message",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="reactions",
                        to="chat.message",
                        verbose_name="Сообщение",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="message_reactions",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Пользователь",
                    ),
                ),
            ],
            options={
                "verbose_name": "Реакция на сообщение",
                "verbose_name_plural": "Реакции на сообщения",
                "db_table": "message_reactions",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["message"], name="message_rea_message_3fb393_idx"
                    ),
                    models.Index(
                        fields=["user"], name="message_rea_user_id_565363_idx"
                    ),
                ],
                "unique_together": {("message", "user", "emoji")},
            },
        ),
        migrations.AddIndex(
            model_name="message",
            index=models.Index(
                fields=["chat", "created_at"], name="messages_chat_id_ec31ea_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="message",
            index=models.Index(fields=["sender"], name="messages_sender__6ae55a_idx"),
        ),
        migrations.AddIndex(
            model_name="message",
            index=models.Index(
                fields=["is_deleted"], name="messages_is_dele_54348c_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="chatparticipant",
            index=models.Index(
                fields=["user", "chat"], name="chat_partic_user_id_69302d_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="chatparticipant",
            index=models.Index(
                fields=["chat", "user"], name="chat_partic_chat_id_e53f2b_idx"
            ),
        ),
        migrations.AlterUniqueTogether(
            name="chatparticipant",
            unique_together={("chat", "user")},
        ),
        migrations.AddIndex(
            model_name="chat",
            index=models.Index(fields=["chat_type"], name="chats_chat_ty_9d2e2e_idx"),
        ),
        migrations.AddIndex(
            model_name="chat",
            index=models.Index(fields=["is_archived"], name="chats_is_arch_cae907_idx"),
        ),
        migrations.AddIndex(
            model_name="chat",
            index=models.Index(
                fields=["last_message_at"], name="chats_last_me_63c84f_idx"
            ),
        ),
    ]
