# Generated by Django 5.2.14 on 2026-06-15 08:02

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("annotations", "0085_reseed_authority_namespaces"),
        ("documents", "0042_pendingcorpusimport"),
    ]

    operations = [
        migrations.CreateModel(
            name="AuthorityKeyEquivalence",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("from_key", models.CharField(max_length=255)),
                ("to_key", models.CharField(max_length=255)),
                (
                    "source",
                    models.CharField(
                        choices=[
                            ("uslm", "OLRC USLM sourceCredit"),
                            ("popular_name", "USC popular-name table"),
                            ("manual", "Hand-curated"),
                        ],
                        default="uslm",
                        max_length=32,
                    ),
                ),
                ("confidence", models.FloatField(default=1.0)),
                ("note", models.CharField(blank=True, max_length=255, null=True)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
            ],
            options={
                "indexes": [
                    models.Index(
                        fields=["from_key"], name="annotations_from_ke_734e02_idx"
                    ),
                    models.Index(
                        fields=["to_key"], name="annotations_to_key_9b7b16_idx"
                    ),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("from_key", "to_key"),
                        name="authority_key_equiv_unique_pair",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="AuthorityFrontier",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "canonical_key",
                    models.CharField(max_length=255, unique=True),
                ),
                ("authority", models.CharField(db_index=True, max_length=64)),
                (
                    "jurisdiction",
                    models.CharField(
                        blank=True, db_index=True, max_length=64, null=True
                    ),
                ),
                (
                    "authority_type",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("statute", "statute"),
                            ("regulation", "regulation"),
                            ("admin-rule", "admin-rule"),
                            ("municipal-ordinance", "municipal-ordinance"),
                            ("case", "case"),
                            ("constitution", "constitution"),
                            ("court-rule", "court-rule"),
                            ("guidance", "guidance"),
                            ("treaty", "treaty"),
                        ],
                        db_index=True,
                        max_length=32,
                        null=True,
                    ),
                ),
                ("mention_count", models.IntegerField(db_index=True, default=0)),
                ("distinct_corpus_count", models.IntegerField(default=0)),
                ("candidate_sources", models.JSONField(blank=True, default=list)),
                (
                    "discovery_state",
                    models.CharField(
                        choices=[
                            ("queued", "Queued"),
                            ("in_progress", "In progress"),
                            ("discovered", "Source found, awaiting ingestion"),
                            ("ingested", "Document imported"),
                            ("resolved", "CorpusReference upgraded to RESOLVED"),
                            ("failed", "No source found"),
                            ("unsupported", "No provider can_handle"),
                        ],
                        db_index=True,
                        default="queued",
                        max_length=32,
                    ),
                ),
                ("depth", models.IntegerField(default=0)),
                (
                    "provider",
                    models.CharField(
                        blank=True, db_index=True, max_length=64, null=True
                    ),
                ),
                ("last_error", models.TextField(blank=True, null=True)),
                ("last_attempt", models.DateTimeField(blank=True, null=True)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("modified", models.DateTimeField(auto_now=True)),
                (
                    "ingested_document",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="frontier_entries",
                        to="documents.document",
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(
                        fields=["-mention_count", "discovery_state"],
                        name="annotations_mention_036018_idx",
                    ),
                    models.Index(
                        fields=["provider", "discovery_state"],
                        name="annotations_provide_55004d_idx",
                    ),
                    models.Index(
                        fields=["jurisdiction", "authority_type"],
                        name="annotations_jurisdi_6617fd_idx",
                    ),
                ],
                "constraints": [
                    models.CheckConstraint(
                        condition=models.Q(
                            ("discovery_state", "queued"),
                            ("ingested_document__isnull", False),
                            _negated=True,
                        ),
                        name="frontier_queued_no_ingested_doc",
                    )
                ],
            },
        ),
    ]
