# Generated by Django 4.2.24 on 2025-12-14 18:08

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


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("corpuses", "0028_fix_grant_permission_to_corpus_creators"),
        ("conversations", "0012_update_chat_field_constraint_for_threads"),
        ("documents", "0026_add_structural_annotation_set"),
        ("auth", "0012_alter_user_first_name_max_length"),
        ("agents", "0005_generate_missing_slugs"),
    ]

    operations = [
        migrations.CreateModel(
            name="AgentActionResult",
            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)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("running", "Running"),
                            ("completed", "Completed"),
                            ("failed", "Failed"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("started_at", models.DateTimeField(blank=True, null=True)),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                (
                    "agent_response",
                    models.TextField(
                        blank=True, help_text="Final response content from the agent"
                    ),
                ),
                (
                    "tools_executed",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="List of tools executed: [{name, args, result, timestamp}]",
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True, help_text="Error message if status is FAILED"
                    ),
                ),
                (
                    "execution_metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Additional execution metadata (model used, token counts, etc.)",
                    ),
                ),
                (
                    "conversation",
                    models.ForeignKey(
                        blank=True,
                        help_text="Conversation record containing the full agent interaction",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="corpus_action_results",
                        to="conversations.conversation",
                    ),
                ),
                (
                    "corpus_action",
                    models.ForeignKey(
                        help_text="The corpus action that triggered this execution",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="agent_results",
                        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 run on",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="agent_action_results",
                        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={
                "ordering": ["-started_at"],
                "permissions": (
                    ("permission_agentactionresult", "permission agentactionresult"),
                    ("publish_agentactionresult", "publish agentactionresult"),
                    ("create_agentactionresult", "create agentactionresult"),
                    ("read_agentactionresult", "read agentactionresult"),
                    ("update_agentactionresult", "update agentactionresult"),
                    ("remove_agentactionresult", "delete agentactionresult"),
                ),
            },
        ),
        migrations.CreateModel(
            name="AgentActionResultUserObjectPermission",
            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="agents.agentactionresult",
                    ),
                ),
                (
                    "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="AgentActionResultGroupObjectPermission",
            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="agents.agentactionresult",
                    ),
                ),
                (
                    "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="agentactionresult",
            index=models.Index(
                fields=["corpus_action", "document"],
                name="agents_agen_corpus__2daffe_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="agentactionresult",
            index=models.Index(fields=["status"], name="agents_agen_status_0477f1_idx"),
        ),
        migrations.AddIndex(
            model_name="agentactionresult",
            index=models.Index(
                fields=["started_at"], name="agents_agen_started_c64191_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="agentactionresult",
            constraint=models.UniqueConstraint(
                fields=("corpus_action", "document"),
                name="unique_corpus_action_document_result",
            ),
        ),
    ]
