# Generated by Django 6.0 on 2026-05-15 19:06

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0380_evalruntask_debug_artifacts'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='PersistentAgentDiscordGuild',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('guild_id', models.CharField(max_length=32)),
                ('name', models.CharField(max_length=255)),
                ('icon_hash', models.CharField(blank=True, max_length=128)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('last_synced_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('claimed_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='claimed_discord_guilds', to=settings.AUTH_USER_MODEL)),
                ('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='discord_guild_claims', to='api.organization')),
                ('owner_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='discord_guild_claims', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['name', 'guild_id'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentDiscordChannelSubscription',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('channel_id', models.CharField(max_length=32)),
                ('channel_name', models.CharField(blank=True, max_length=255)),
                ('status', models.CharField(choices=[('active', 'Active'), ('disabled', 'Disabled'), ('error', 'Error')], db_index=True, default='active', max_length=16)),
                ('last_message_at', models.DateTimeField(blank=True, null=True)),
                ('last_error', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='discord_channel_subscriptions', to='api.persistentagent')),
                ('guild', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='channel_subscriptions', to='api.persistentagentdiscordguild')),
            ],
            options={
                'ordering': ['guild__name', 'channel_name', 'channel_id'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentDiscordOAuthSession',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('state', models.CharField(max_length=128, unique=True)),
                ('expires_at', models.DateTimeField()),
                ('completed_at', models.DateTimeField(blank=True, null=True)),
                ('selected_guild_id', models.CharField(blank=True, max_length=32)),
                ('selected_permissions', models.CharField(blank=True, max_length=64)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='discord_oauth_sessions', to='api.persistentagent')),
                ('initiated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='initiated_discord_oauth_sessions', to=settings.AUTH_USER_MODEL)),
                ('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='discord_oauth_sessions', to='api.organization')),
                ('owner_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='discord_oauth_sessions', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='PersistentAgentDiscordWebhook',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('channel_id', models.CharField(max_length=32)),
                ('webhook_id', models.CharField(max_length=32)),
                ('webhook_token_encrypted', models.BinaryField(blank=True, null=True)),
                ('name', models.CharField(blank=True, max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('guild', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='channel_webhooks', to='api.persistentagentdiscordguild')),
            ],
            options={
                'ordering': ['guild__name', 'channel_id'],
            },
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordguild',
            index=models.Index(fields=['owner_user', 'is_active'], name='pa_discord_guild_user_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordguild',
            index=models.Index(fields=['organization', 'is_active'], name='pa_discord_guild_org_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordguild',
            index=models.Index(fields=['guild_id'], name='pa_discord_guild_id_idx'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentdiscordguild',
            constraint=models.UniqueConstraint(condition=models.Q(('is_active', True)), fields=('guild_id',), name='uniq_active_discord_guild_claim'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentdiscordguild',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('organization__isnull', True), ('owner_user__isnull', False)), models.Q(('organization__isnull', False), ('owner_user__isnull', True)), _connector='OR'), name='pa_discord_guild_one_owner'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordchannelsubscription',
            index=models.Index(fields=['agent', 'status'], name='pa_discord_sub_agent_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordchannelsubscription',
            index=models.Index(fields=['guild', 'channel_id'], name='pa_discord_sub_channel_idx'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentdiscordchannelsubscription',
            constraint=models.UniqueConstraint(condition=models.Q(('status', 'active')), fields=('agent', 'guild', 'channel_id'), name='uniq_active_discord_agent_channel_sub'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordoauthsession',
            index=models.Index(fields=['state'], name='pa_discord_oauth_state_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordoauthsession',
            index=models.Index(fields=['agent', 'expires_at'], name='pa_discord_oauth_agent_idx'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentdiscordoauthsession',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('organization__isnull', True), ('owner_user__isnull', False)), models.Q(('organization__isnull', False), ('owner_user__isnull', True)), _connector='OR'), name='pa_discord_oauth_one_owner'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordwebhook',
            index=models.Index(fields=['guild', 'channel_id'], name='pa_discord_wh_channel_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentdiscordwebhook',
            index=models.Index(fields=['webhook_id'], name='pa_discord_wh_id_idx'),
        ),
        migrations.AddConstraint(
            model_name='persistentagentdiscordwebhook',
            constraint=models.UniqueConstraint(fields=('guild', 'channel_id'), name='uniq_discord_webhook_channel'),
        ),
    ]
