# 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 functools
import opencontractserver.shared.defaults
import opencontractserver.shared.fields
import opencontractserver.shared.utils


class Migration(migrations.Migration):

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

    operations = [
        migrations.AddField(
            model_name="document",
            name="md_summary_file",
            field=models.FileField(
                blank=True,
                max_length=1024,
                null=True,
                upload_to=functools.partial(
                    opencontractserver.shared.utils.calc_oc_file_path,
                    *(),
                    **{"sub_folder": "md_summaries"}
                ),
            ),
        ),
        migrations.CreateModel(
            name="DocumentRelationship",
            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)),
                (
                    "relationship_type",
                    models.CharField(
                        choices=[("NOTES", "Notes"), ("RELATIONSHIP", "Relationship")],
                        default="RELATIONSHIP",
                        max_length=32,
                    ),
                ),
                (
                    "data",
                    opencontractserver.shared.fields.NullableJSONField(
                        blank=True,
                        default=opencontractserver.shared.defaults.jsonfield_default_value,
                        null=True,
                    ),
                ),
                (
                    "annotation_label",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="document_relationships",
                        to="annotations.annotationlabel",
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="document_relationships",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "source_document",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="source_relationships",
                        to="documents.document",
                    ),
                ),
                (
                    "target_document",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="target_relationships",
                        to="documents.document",
                    ),
                ),
                (
                    "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_documentrelationship",
                        "permission document relationship",
                    ),
                    ("publish_documentrelationship", "publish document relationship"),
                    ("create_documentrelationship", "create document relationship"),
                    ("read_documentrelationship", "read document relationship"),
                    ("update_documentrelationship", "update document relationship"),
                    ("remove_documentrelationship", "delete document relationship"),
                ),
            },
        ),
        migrations.CreateModel(
            name="DocumentRelationshipUserObjectPermission",
            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="documents.documentrelationship",
                    ),
                ),
                (
                    "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="DocumentRelationshipGroupObjectPermission",
            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="documents.documentrelationship",
                    ),
                ),
                (
                    "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.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["source_document"], name="documents_d_source__a6939d_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["target_document"], name="documents_d_target__84a3d2_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["relationship_type"], name="documents_d_relatio_ce06b8_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["annotation_label"], name="documents_d_annotat_2c6ad8_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["creator"], name="documents_d_creator_51e304_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["created"], name="documents_d_created_406b6c_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentrelationship",
            index=models.Index(
                fields=["modified"], name="documents_d_modifie_f9bcc6_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="documentrelationship",
            constraint=models.UniqueConstraint(
                condition=models.Q(("relationship_type", "RELATIONSHIP")),
                fields=("source_document", "target_document", "annotation_label"),
                name="unique_document_relationship",
            ),
        ),
    ]
