# Generated by Django 5.2.14 on 2026-06-09 15:39

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


class Migration(migrations.Migration):

    dependencies = [
        ("analyzer", "0022_alter_analysis_status"),
        ("annotations", "0077_annotation_structural_modified_index"),
        ("corpuses", "0056_corpus_auto_branding_enabled"),
        ("documents", "0042_pendingcorpusimport"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="CorpusReference",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("backend_lock", models.BooleanField(db_index=True, default=False)),
                ("is_public", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "reference_type",
                    models.CharField(
                        choices=[
                            ("LAW", "Law citation"),
                            ("DOCUMENT", "Document reference"),
                            ("SECTION", "Internal section reference"),
                            ("DEFINED_TERM", "Defined term"),
                        ],
                        db_index=True,
                        max_length=16,
                    ),
                ),
                (
                    "canonical_key",
                    models.CharField(
                        blank=True, db_index=True, max_length=255, null=True
                    ),
                ),
                ("normalized_data", models.JSONField(blank=True, null=True)),
                ("confidence", models.FloatField(default=1.0)),
                (
                    "resolution_status",
                    models.CharField(
                        choices=[
                            ("RESOLVED", "Resolved"),
                            ("UNRESOLVED", "Unresolved"),
                            ("EXTERNAL", "External (no internal target)"),
                        ],
                        default="RESOLVED",
                        max_length=16,
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="references",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "created_by_analysis",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_references",
                        to="analyzer.analysis",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "source_annotation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="outbound_references",
                        to="annotations.annotation",
                    ),
                ),
                (
                    "target_annotation",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="inbound_references",
                        to="annotations.annotation",
                    ),
                ),
                (
                    "target_corpus",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="inbound_references",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "target_document",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="inbound_references",
                        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={
                "indexes": [
                    models.Index(
                        fields=["corpus", "reference_type"],
                        name="annotations_corpus__a5d62c_idx",
                    ),
                    models.Index(
                        fields=["canonical_key"], name="annotations_canonic_92d0e7_idx"
                    ),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("source_annotation", "reference_type", "canonical_key"),
                        name="uniq_corpusref_source_type_key",
                    )
                ],
            },
            bases=(
                opencontractserver.shared.user_can_mixin.InstanceUserCanMixin,
                models.Model,
            ),
        ),
    ]
