# Generated by Django 4.2.24 on 2025-11-17 17:20

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


class Migration(migrations.Migration):

    dependencies = [
        ("documents", "0025_add_source_document_provenance"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("annotations", "0047_add_conversation_message_embeddings"),
    ]

    operations = [
        migrations.CreateModel(
            name="StructuralAnnotationSet",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("backend_lock", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "content_hash",
                    models.CharField(
                        db_index=True,
                        help_text="SHA-256 hash of the document content this set belongs to",
                        max_length=64,
                        unique=True,
                    ),
                ),
                (
                    "parser_name",
                    models.CharField(
                        blank=True,
                        help_text="Parser class used",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "parser_version",
                    models.CharField(
                        blank=True, help_text="Parser version", max_length=50, null=True
                    ),
                ),
                (
                    "page_count",
                    models.IntegerField(
                        blank=True,
                        help_text="Number of pages in the document",
                        null=True,
                    ),
                ),
                (
                    "token_count",
                    models.IntegerField(
                        blank=True,
                        help_text="Total number of tokens extracted",
                        null=True,
                    ),
                ),
                (
                    "pawls_parse_file",
                    models.FileField(
                        blank=True,
                        help_text="PAWLS JSON parse data for this content",
                        max_length=1024,
                        null=True,
                        upload_to=opencontractserver.shared.utils.calc_oc_file_path,
                    ),
                ),
                (
                    "txt_extract_file",
                    models.FileField(
                        blank=True,
                        help_text="Plain text extraction for this content",
                        max_length=1024,
                        null=True,
                        upload_to=opencontractserver.shared.utils.calc_oc_file_path,
                    ),
                ),
                ("is_public", models.BooleanField(default=True)),
            ],
        ),
        migrations.AlterField(
            model_name="annotation",
            name="document",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="doc_annotations",
                related_query_name="doc_annotation",
                to="documents.document",
            ),
        ),
        migrations.AlterField(
            model_name="relationship",
            name="document",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="relationships",
                to="documents.document",
            ),
        ),
        migrations.AddField(
            model_name="structuralannotationset",
            name="creator",
            field=models.ForeignKey(
                blank=True,
                help_text="User who first parsed this content",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="structuralannotationset",
            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.AddField(
            model_name="annotation",
            name="structural_set",
            field=models.ForeignKey(
                blank=True,
                help_text="For structural annotations shared across documents",
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="structural_annotations",
                to="annotations.structuralannotationset",
            ),
        ),
        migrations.AddField(
            model_name="relationship",
            name="structural_set",
            field=models.ForeignKey(
                blank=True,
                help_text="For structural relationships shared across documents",
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="structural_relationships",
                to="annotations.structuralannotationset",
            ),
        ),
        migrations.AddIndex(
            model_name="structuralannotationset",
            index=models.Index(
                fields=["content_hash"], name="annotations_content_1f8816_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="structuralannotationset",
            index=models.Index(
                fields=["parser_name"], name="annotations_parser__9bd3b1_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="structuralannotationset",
            index=models.Index(
                fields=["created"], name="annotations_created_67c8af_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="annotation",
            index=models.Index(
                fields=["structural_set"], name="annotations_structu_3ad1ab_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="relationship",
            index=models.Index(
                fields=["structural_set"], name="annotations_structu_dbe65e_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="annotation",
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(
                        ("document__isnull", False), ("structural_set__isnull", True)
                    ),
                    models.Q(
                        ("document__isnull", True), ("structural_set__isnull", False)
                    ),
                    _connector="OR",
                ),
                name="annotation_has_single_parent",
                violation_error_message="An annotation must belong to either a document or a structural set, not both or neither",
            ),
        ),
        migrations.AddConstraint(
            model_name="relationship",
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(
                        ("document__isnull", False), ("structural_set__isnull", True)
                    ),
                    models.Q(
                        ("document__isnull", True), ("structural_set__isnull", False)
                    ),
                    _connector="OR",
                ),
                name="relationship_has_single_parent",
                violation_error_message="A relationship must belong to either a document or a structural set, not both or neither",
            ),
        ),
    ]
