# Generated by Django 4.2.18 on 2025-01-26 07:58

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import opencontractserver.shared.defaults
import opencontractserver.shared.fields


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("corpuses", "0014_alter_corpus_label_set"),
        ("documents", "0011_alter_document_page_count"),
        ("annotations", "0026_note_noteuserobjectpermission_and_more"),
        ("auth", "0012_alter_user_first_name_max_length"),
    ]

    operations = [
        migrations.CreateModel(
            name="ChatMessage",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("backend_lock", models.BooleanField(default=False)),
                ("is_public", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "msg_type",
                    models.CharField(
                        choices=[
                            ("SYSTEM", "SYSTEM"),
                            ("HUMAN", "HUMAN"),
                            ("LLM", "LLM"),
                        ],
                        help_text="The type of message (SYSTEM, HUMAN, or LLM)",
                        max_length=32,
                    ),
                ),
                (
                    "content",
                    models.TextField(
                        help_text="The textual content of the chat message"
                    ),
                ),
                (
                    "data",
                    opencontractserver.shared.fields.NullableJSONField(
                        blank=True,
                        default=opencontractserver.shared.defaults.jsonfield_default_value,
                        help_text="Additional data associated with the chat message (stored as JSON)",
                        null=True,
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Timestamp when the chat message was created",
                    ),
                ),
            ],
            options={
                "permissions": (
                    ("permission_chatmessage", "permission chatmessage"),
                    ("publish_chatmessage", "publish chatmessage"),
                    ("create_chatmessage", "create chatmessage"),
                    ("read_chatmessage", "read chatmessage"),
                    ("update_chatmessage", "update chatmessage"),
                    ("remove_chatmessage", "delete chatmessage"),
                ),
            },
        ),
        migrations.CreateModel(
            name="Conversation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("backend_lock", models.BooleanField(default=False)),
                ("is_public", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "title",
                    models.CharField(
                        blank=True,
                        help_text="Optional title for the conversation",
                        max_length=255,
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Timestamp when the conversation was created",
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True,
                        help_text="Timestamp when the conversation was last updated",
                    ),
                ),
                (
                    "chat_with_corpus",
                    models.ForeignKey(
                        blank=True,
                        help_text="The corpus to which this conversation belongs",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="conversations",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "chat_with_document",
                    models.ForeignKey(
                        blank=True,
                        help_text="The document to which this conversation belongs",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="conversations",
                        to="documents.document",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user_lock",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="locked_%(class)s_objects",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "permissions": (
                    ("permission_conversation", "permission conversation"),
                    ("publish_conversation", "publish conversation"),
                    ("create_conversation", "create conversation"),
                    ("read_conversation", "read conversation"),
                    ("update_conversation", "update conversation"),
                    ("remove_conversation", "delete conversation"),
                ),
            },
        ),
        migrations.CreateModel(
            name="ChatMessageUserObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.chatmessage",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="ChatMessageGroupObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.chatmessage",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="auth.group"
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="conversation",
            field=models.ForeignKey(
                help_text="The conversation to which this chat message belongs",
                on_delete=django.db.models.deletion.CASCADE,
                related_name="chat_messages",
                to="conversations.conversation",
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="created_annotations",
            field=models.ManyToManyField(
                blank=True,
                help_text="Annotations that this chat message created",
                related_name="created_by_chat_message",
                to="annotations.annotation",
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="creator",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="source_annotations",
            field=models.ManyToManyField(
                blank=True,
                help_text="Annotations that this chat message is based on",
                related_name="chat_messages",
                to="annotations.annotation",
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="source_document",
            field=models.ForeignKey(
                blank=True,
                help_text="A document that this chat message is based on",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="chat_messages",
                to="documents.document",
            ),
        ),
        migrations.AddField(
            model_name="chatmessage",
            name="user_lock",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="locked_%(class)s_objects",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.CreateModel(
            name="ConversationUserObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.conversation",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
                "unique_together": {("user", "permission", "content_object")},
            },
        ),
        migrations.CreateModel(
            name="ConversationGroupObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "content_object",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="conversations.conversation",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="auth.group"
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.permission",
                    ),
                ),
            ],
            options={
                "abstract": False,
                "unique_together": {("group", "permission", "content_object")},
            },
        ),
        migrations.AddConstraint(
            model_name="conversation",
            constraint=models.CheckConstraint(
                check=models.Q(
                    ("chat_with_corpus__isnull", True),
                    ("chat_with_document__isnull", True),
                    _connector="OR",
                ),
                name="one_chat_field_null_constraint",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="chatmessageuserobjectpermission",
            unique_together={("user", "permission", "content_object")},
        ),
        migrations.AlterUniqueTogether(
            name="chatmessagegroupobjectpermission",
            unique_together={("group", "permission", "content_object")},
        ),
    ]
