# Generated by Django 5.2 on 2025-10-21 13:09

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0167_add_proactive_agent_rollout_flag'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='MCPServerConfig',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('scope', models.CharField(choices=[('platform', 'Platform'), ('organization', 'Organization'), ('user', 'User')], max_length=32)),
                ('name', models.SlugField(max_length=64)),
                ('display_name', models.CharField(max_length=128)),
                ('description', models.TextField(blank=True)),
                ('command', models.CharField(blank=True, max_length=255)),
                ('command_args', models.JSONField(blank=True, default=list)),
                ('url', models.CharField(blank=True, max_length=512)),
                ('prefetch_apps', models.JSONField(blank=True, default=list)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('env_json_encrypted', models.BinaryField(blank=True, null=True)),
                ('headers_json_encrypted', models.BinaryField(blank=True, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_server_configs', to='api.organization')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_server_configs', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.AddField(
            model_name='persistentagentenabledtool',
            name='server_config',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='enabled_tools', to='api.mcpserverconfig'),
        ),
        migrations.CreateModel(
            name='PersistentAgentMCPServer',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='personal_mcp_servers', to='api.persistentagent')),
                ('server_config', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='agent_assignments', to='api.mcpserverconfig')),
            ],
        ),
        migrations.AddIndex(
            model_name='mcpserverconfig',
            index=models.Index(fields=['scope', 'is_active'], name='mcp_server_scope_active_idx'),
        ),
        migrations.AddIndex(
            model_name='mcpserverconfig',
            index=models.Index(fields=['organization', 'name'], name='mcp_server_org_name_idx'),
        ),
        migrations.AddIndex(
            model_name='mcpserverconfig',
            index=models.Index(fields=['user', 'name'], name='mcp_server_user_name_idx'),
        ),
        migrations.AddConstraint(
            model_name='mcpserverconfig',
            constraint=models.UniqueConstraint(condition=models.Q(('scope', 'platform')), fields=('scope', 'name'), name='unique_platform_mcp_server_name'),
        ),
        migrations.AddConstraint(
            model_name='mcpserverconfig',
            constraint=models.UniqueConstraint(condition=models.Q(('scope', 'organization')), fields=('organization', 'name'), name='unique_org_mcp_server_name'),
        ),
        migrations.AddConstraint(
            model_name='mcpserverconfig',
            constraint=models.UniqueConstraint(condition=models.Q(('scope', 'user')), fields=('user', 'name'), name='unique_user_mcp_server_name'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmcpserver',
            index=models.Index(fields=['agent', 'server_config'], name='agent_personal_server_idx'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentmcpserver',
            constraint=models.UniqueConstraint(fields=('agent', 'server_config'), name='unique_agent_personal_server'),
        ),
    ]
