# Generated by Django 4.2.24 on 2025-11-07 01:47

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("corpuses", "0023_corpusengagementmetrics"),
        ("auth", "0012_alter_user_first_name_max_length"),
    ]

    operations = [
        migrations.CreateModel(
            name="AgentConfiguration",
            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)),
                (
                    "name",
                    models.CharField(
                        help_text="Display name for this agent", max_length=255
                    ),
                ),
                (
                    "description",
                    models.TextField(
                        blank=True,
                        help_text="Description of agent's purpose and capabilities",
                    ),
                ),
                (
                    "system_instructions",
                    models.TextField(
                        help_text="System prompt/instructions for this agent"
                    ),
                ),
                (
                    "available_tools",
                    models.JSONField(
                        default=list,
                        help_text="List of tool identifiers this agent can use (e.g., ['similarity_search', 'load_document_text'])",
                    ),
                ),
                (
                    "permission_required_tools",
                    models.JSONField(
                        default=list,
                        help_text="Subset of tools that require explicit user permission to use",
                    ),
                ),
                (
                    "badge_config",
                    models.JSONField(
                        default=dict,
                        help_text="Visual config: {'icon': 'bot', 'color': '#4A90E2', 'label': 'AI Assistant'}",
                    ),
                ),
                (
                    "avatar_url",
                    models.URLField(
                        blank=True, help_text="URL to agent's avatar image", null=True
                    ),
                ),
                (
                    "scope",
                    models.CharField(
                        choices=[("GLOBAL", "Global"), ("CORPUS", "Corpus-specific")],
                        default="GLOBAL",
                        max_length=10,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True,
                        help_text="Whether this agent is active and can be used",
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        blank=True,
                        help_text="Corpus this agent belongs to (if scope=CORPUS)",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="agents",
                        to="corpuses.corpus",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        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={
                "permissions": (
                    ("permission_agentconfiguration", "permission agentconfiguration"),
                    ("publish_agentconfiguration", "publish agentconfiguration"),
                    ("create_agentconfiguration", "create agentconfiguration"),
                    ("read_agentconfiguration", "read agentconfiguration"),
                    ("update_agentconfiguration", "update agentconfiguration"),
                    ("remove_agentconfiguration", "delete agentconfiguration"),
                ),
            },
        ),
        migrations.CreateModel(
            name="AgentConfigurationUserObjectPermission",
            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.agentconfiguration",
                    ),
                ),
                (
                    "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="AgentConfigurationGroupObjectPermission",
            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.agentconfiguration",
                    ),
                ),
                (
                    "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="agentconfiguration",
            index=models.Index(
                fields=["scope", "is_active"], name="agents_agen_scope_de97b5_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="agentconfiguration",
            index=models.Index(
                fields=["corpus", "is_active"], name="agents_agen_corpus__252f39_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="agentconfiguration",
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(("corpus__isnull", True), ("scope", "GLOBAL")),
                    models.Q(("corpus__isnull", False), ("scope", "CORPUS")),
                    _connector="OR",
                ),
                name="agent_scope_corpus_consistency",
            ),
        ),
    ]
