# Generated by Django 4.2.24 on 2025-11-14 09:47

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


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("auth", "0012_alter_user_first_name_max_length"),
        ("corpuses", "0025_corpusfolder_is_public"),
        ("documents", "0022_alter_documentanalysisrow_options_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="DocumentPath",
            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)),
                (
                    "path",
                    models.CharField(
                        db_index=True,
                        help_text="Full path in corpus filesystem",
                        max_length=1024,
                    ),
                ),
                (
                    "version_number",
                    models.IntegerField(
                        help_text="Content version number (Rule P5: increments only on content changes)"
                    ),
                ),
                (
                    "is_deleted",
                    models.BooleanField(
                        db_index=True, default=False, help_text="Soft delete flag"
                    ),
                ),
                (
                    "is_current",
                    models.BooleanField(
                        db_index=True,
                        default=True,
                        help_text="True for current filesystem state (Rule P3)",
                    ),
                ),
            ],
            options={
                "permissions": (
                    ("create_documentpath", "create DocumentPath"),
                    ("read_documentpath", "read DocumentPath"),
                    ("update_documentpath", "update DocumentPath"),
                    ("remove_documentpath", "delete DocumentPath"),
                ),
            },
        ),
        migrations.CreateModel(
            name="DocumentPathGroupObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="DocumentPathUserObjectPermission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="document",
            name="is_current",
            field=models.BooleanField(
                db_index=True,
                default=True,
                help_text="True for newest content in this version tree. Implements Rule C3.",
            ),
        ),
        migrations.AddField(
            model_name="document",
            name="parent",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="children",
                to="documents.document",
                verbose_name="parent",
            ),
        ),
        migrations.AddField(
            model_name="document",
            name="version_tree_id",
            field=models.UUIDField(
                db_index=True,
                null=True,  # Allow null initially - will be populated by 0024 data migration
                help_text="Groups all content versions of same logical document. Implements Rule C1.",
            ),
        ),
        # NOTE: The UniqueConstraint "one_current_per_version_tree" is added in migration 0024
        # AFTER the data migration populates unique UUIDs for each document.
        # This prevents the constraint from failing when all existing rows get the same
        # default UUID value during AddField.
        migrations.AddField(
            model_name="documentpathuserobjectpermission",
            name="content_object",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="documents.documentpath"
            ),
        ),
        migrations.AddField(
            model_name="documentpathuserobjectpermission",
            name="permission",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="auth.permission"
            ),
        ),
        migrations.AddField(
            model_name="documentpathuserobjectpermission",
            name="user",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
            ),
        ),
        migrations.AddField(
            model_name="documentpathgroupobjectpermission",
            name="content_object",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="documents.documentpath"
            ),
        ),
        migrations.AddField(
            model_name="documentpathgroupobjectpermission",
            name="group",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="auth.group"
            ),
        ),
        migrations.AddField(
            model_name="documentpathgroupobjectpermission",
            name="permission",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="auth.permission"
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            name="corpus",
            field=models.ForeignKey(
                help_text="Corpus owning this path",
                on_delete=django.db.models.deletion.CASCADE,
                related_name="document_paths",
                to="corpuses.corpus",
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            name="creator",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            name="document",
            field=models.ForeignKey(
                help_text="Specific content version this path points to",
                on_delete=django.db.models.deletion.PROTECT,
                related_name="path_records",
                to="documents.document",
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            name="folder",
            field=models.ForeignKey(
                blank=True,
                help_text="Current folder (null if folder deleted or at root)",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="document_paths",
                to="corpuses.corpusfolder",
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            name="parent",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="children",
                to="documents.documentpath",
                verbose_name="parent",
            ),
        ),
        migrations.AddField(
            model_name="documentpath",
            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.AlterUniqueTogether(
            name="documentpathuserobjectpermission",
            unique_together={("user", "permission", "content_object")},
        ),
        migrations.AlterUniqueTogether(
            name="documentpathgroupobjectpermission",
            unique_together={("group", "permission", "content_object")},
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(
                fields=["corpus", "is_current", "is_deleted"],
                name="documents_d_corpus__cae304_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(
                fields=["document", "corpus"], name="documents_d_documen_2980ac_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(fields=["path"], name="documents_d_path_0ec51e_idx"),
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(
                fields=["version_number"], name="documents_d_version_4f5cef_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(
                fields=["creator"], name="documents_d_creator_a09abd_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="documentpath",
            index=models.Index(
                fields=["created"], name="documents_d_created_7ffa67_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="documentpath",
            constraint=models.UniqueConstraint(
                condition=models.Q(("is_current", True), ("is_deleted", False)),
                fields=("corpus", "path"),
                name="unique_active_path_per_corpus",
            ),
        ),
    ]
