# Generated by Django 4.2.24 on 2025-12-20 16:43

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


class Migration(migrations.Migration):

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        ("documents", "0027_alter_document_backend_lock_and_more"),
        ("extracts", "0026_alter_column_backend_lock_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("analyzer", "0018_alter_analysis_backend_lock_and_more"),
        ("agents", "0007_alter_agentactionresult_backend_lock_and_more"),
        ("corpuses", "0030_alter_corpusaction_backend_lock_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="CorpusActionExecution",
            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)),
                (
                    "action_type",
                    models.CharField(
                        choices=[
                            ("fieldset", "Fieldset Extract"),
                            ("analyzer", "Analyzer"),
                            ("agent", "Agent"),
                        ],
                        db_index=True,
                        help_text="Type of action (fieldset/analyzer/agent)",
                        max_length=20,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("queued", "Queued"),
                            ("running", "Running"),
                            ("completed", "Completed"),
                            ("failed", "Failed"),
                            ("skipped", "Skipped"),
                        ],
                        db_index=True,
                        default="queued",
                        max_length=20,
                    ),
                ),
                (
                    "queued_at",
                    models.DateTimeField(
                        db_index=True,
                        help_text="When the execution was queued (set explicitly for bulk_create)",
                    ),
                ),
                (
                    "started_at",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        help_text="When execution actually started",
                        null=True,
                    ),
                ),
                (
                    "completed_at",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        help_text="When execution completed (success or failure)",
                        null=True,
                    ),
                ),
                (
                    "trigger",
                    models.CharField(
                        choices=[
                            ("add_document", "Add Document"),
                            ("edit_document", "Edit Document"),
                        ],
                        help_text="What triggered this execution",
                        max_length=128,
                    ),
                ),
                (
                    "affected_objects",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='\n        List of objects created or modified by this execution.\n        Format: [\n            {"type": "extract", "id": 123},\n            {"type": "datacell", "id": 456, "column_name": "parties"},\n            {"type": "analysis", "id": 789},\n            {"type": "annotation", "id": 101, "label": "indemnification"},\n            {"type": "document_summary", "revision_id": 202},\n            {"type": "document_meta", "field": "description", "old": "...", "new": "..."},\n        ]\n        ',
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Error message if status is FAILED",
                    ),
                ),
                (
                    "error_traceback",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Full traceback for debugging (truncated to 10KB)",
                    ),
                ),
                (
                    "execution_metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text='\n        Additional execution context:\n        {\n            "model": "gpt-4",\n            "tokens_used": 1500,\n            "retry_count": 0,\n            "celery_task_id": "abc-123",\n            "worker_id": "worker-1",\n        }\n        ',
                    ),
                ),
                (
                    "agent_result",
                    models.ForeignKey(
                        blank=True,
                        help_text="Detailed agent result (for agent actions only)",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="execution_record",
                        to="agents.agentactionresult",
                    ),
                ),
                (
                    "analysis",
                    models.ForeignKey(
                        blank=True,
                        help_text="Analysis created (for analyzer actions only)",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="execution_records",
                        to="analyzer.analysis",
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        help_text="Denormalized corpus reference for fast queries",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="action_executions",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "corpus_action",
                    models.ForeignKey(
                        help_text="The corpus action configuration that was executed",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="executions",
                        to="corpuses.corpusaction",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "document",
                    models.ForeignKey(
                        help_text="The document this action was executed on",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="corpus_action_executions",
                        to="documents.document",
                    ),
                ),
                (
                    "extract",
                    models.ForeignKey(
                        blank=True,
                        help_text="Extract created (for fieldset actions only)",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="execution_records",
                        to="extracts.extract",
                    ),
                ),
                (
                    "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={
                "ordering": ["-queued_at"],
                "permissions": (
                    (
                        "permission_corpusactionexecution",
                        "permission corpusactionexecution",
                    ),
                    ("publish_corpusactionexecution", "publish corpusactionexecution"),
                    ("create_corpusactionexecution", "create corpusactionexecution"),
                    ("read_corpusactionexecution", "read corpusactionexecution"),
                    ("update_corpusactionexecution", "update corpusactionexecution"),
                    ("remove_corpusactionexecution", "delete corpusactionexecution"),
                ),
            },
        ),
        migrations.CreateModel(
            name="CorpusActionExecutionUserObjectPermission",
            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="corpuses.corpusactionexecution",
                    ),
                ),
                (
                    "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="CorpusActionExecutionGroupObjectPermission",
            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="corpuses.corpusactionexecution",
                    ),
                ),
                (
                    "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="corpusactionexecution",
            index=models.Index(
                fields=["corpus", "-queued_at"], name="corpusactionexec_corpus_queue"
            ),
        ),
        migrations.AddIndex(
            model_name="corpusactionexecution",
            index=models.Index(
                fields=["corpus_action", "-queued_at"],
                name="corpusactionexec_action_queue",
            ),
        ),
        migrations.AddIndex(
            model_name="corpusactionexecution",
            index=models.Index(
                fields=["document", "-queued_at"], name="corpusactionexec_doc_queue"
            ),
        ),
        migrations.AddIndex(
            model_name="corpusactionexecution",
            index=models.Index(
                fields=["status", "-queued_at"], name="corpusactionexec_status_queue"
            ),
        ),
        migrations.AddIndex(
            model_name="corpusactionexecution",
            index=models.Index(
                fields=["corpus", "action_type", "-queued_at"],
                name="corpusactionexec_type_queue",
            ),
        ),
        migrations.AddIndex(
            model_name="corpusactionexecution",
            index=models.Index(
                fields=["corpus_action", "document", "status"],
                name="corpusactionexec_dedup",
            ),
        ),
    ]
