# Generated by Django 5.2.11 on 2026-03-04 06:58

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


class Migration(migrations.Migration):

    dependencies = [
        ("agents", "0009_update_checkconstraint_check_to_condition"),
        ("corpuses", "0044_rename_placeholder_indexes"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="CorpusActionTemplate",
            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)),
                ("name", models.CharField(max_length=256, unique=True)),
                ("description", models.TextField(blank=True, default="")),
                (
                    "task_instructions",
                    models.TextField(
                        help_text="What the agent should do when this action fires."
                    ),
                ),
                (
                    "pre_authorized_tools",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="Tools pre-authorized to run without user approval.",
                    ),
                ),
                (
                    "trigger",
                    models.CharField(
                        choices=[
                            ("add_document", "Add Document"),
                            ("edit_document", "Edit Document"),
                            ("new_thread", "New Thread Created"),
                            ("new_message", "New Message Posted"),
                        ],
                        max_length=256,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True,
                        help_text="Whether this template appears in the Action Library for users to add.",
                    ),
                ),
                (
                    "disabled_on_clone",
                    models.BooleanField(
                        default=False,
                        help_text="If True, cloned actions start disabled (user must opt-in).",
                    ),
                ),
                (
                    "sort_order",
                    models.IntegerField(
                        default=0, help_text="Display ordering in template lists."
                    ),
                ),
                (
                    "agent_config",
                    models.ForeignKey(
                        blank=True,
                        help_text="Optional agent configuration for persona/tool defaults.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="action_templates",
                        to="agents.agentconfiguration",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "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": ["sort_order", "name"],
                "indexes": [
                    models.Index(
                        fields=["sort_order", "name"],
                        name="corpuses_actio_sort_or_idx",
                    ),
                ],
            },
        ),
        migrations.AddField(
            model_name="corpusaction",
            name="source_template",
            field=models.ForeignKey(
                blank=True,
                help_text="The template this action was cloned from, if any.",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="cloned_actions",
                to="corpuses.corpusactiontemplate",
            ),
        ),
        migrations.AddConstraint(
            model_name="corpusaction",
            constraint=models.UniqueConstraint(
                condition=models.Q(source_template__isnull=False),
                fields=["corpus", "source_template"],
                name="unique_template_per_corpus",
            ),
        ),
    ]
