# Generated by Django 5.2 on 2025-10-03 00:51

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0150_persistentagentwebsession'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='AgentCommPeerState',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('channel', models.CharField(choices=[('email', 'Email'), ('sms', 'SMS'), ('slack', 'Slack'), ('discord', 'Discord'), ('web', 'Web Chat'), ('other', 'Other')], max_length=32)),
                ('messages_per_window', models.PositiveIntegerField(default=30, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(500)])),
                ('window_hours', models.PositiveIntegerField(default=6, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(168)])),
                ('credits_remaining', models.PositiveIntegerField(default=0)),
                ('window_reset_at', models.DateTimeField()),
                ('last_message_at', models.DateTimeField(blank=True, null=True)),
                ('debounce_seconds', models.PositiveIntegerField(default=5)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.CreateModel(
            name='AgentPeerLink',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('messages_per_window', models.PositiveIntegerField(default=30, help_text='Number of peer messages allowed per rolling window.', validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(500)])),
                ('window_hours', models.PositiveIntegerField(default=6, help_text='Length of the quota window in hours.', validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(168)])),
                ('is_enabled', models.BooleanField(default=True, help_text='Feature-flag style toggle to enable peer messaging for this link.')),
                ('feature_flag', models.CharField(blank=True, help_text='Optional rollout flag label controlling this peer link.', max_length=64)),
                ('pair_key', models.CharField(editable=False, help_text='Deterministic key built from the sorted agent IDs for uniqueness.', max_length=96, unique=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.AddField(
            model_name='persistentagentconversation',
            name='is_peer_dm',
            field=models.BooleanField(default=False, help_text='Whether this conversation stores direct messages between agents.'),
        ),
        migrations.AddField(
            model_name='persistentagentmessage',
            name='peer_agent',
            field=models.ForeignKey(blank=True, help_text='The other agent participating in a peer DM, when applicable.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='peer_agent_messages', to='api.persistentagent'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['peer_agent', '-timestamp'], name='pa_msg_peer_agent_idx'),
        ),
        migrations.AddField(
            model_name='agentpeerlink',
            name='agent_a',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='peer_links_initiated', to='api.persistentagent'),
        ),
        migrations.AddField(
            model_name='agentpeerlink',
            name='agent_a_endpoint',
            field=models.ForeignKey(blank=True, help_text='Preferred endpoint for agent A when initiating peer DMs.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='peer_link_agent_a_endpoints', to='api.persistentagentcommsendpoint'),
        ),
        migrations.AddField(
            model_name='agentpeerlink',
            name='agent_b',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='peer_links_received', to='api.persistentagent'),
        ),
        migrations.AddField(
            model_name='agentpeerlink',
            name='agent_b_endpoint',
            field=models.ForeignKey(blank=True, help_text='Preferred endpoint for agent B when initiating peer DMs.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='peer_link_agent_b_endpoints', to='api.persistentagentcommsendpoint'),
        ),
        migrations.AddField(
            model_name='agentpeerlink',
            name='created_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_agent_peer_links', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='agentcommpeerstate',
            name='link',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='communication_states', to='api.agentpeerlink'),
        ),
        migrations.AddField(
            model_name='persistentagentconversation',
            name='peer_link',
            field=models.OneToOneField(blank=True, help_text='Peer link associated with this direct message thread, when applicable.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='conversation', to='api.agentpeerlink'),
        ),
        migrations.AddIndex(
            model_name='persistentagentconversation',
            index=models.Index(fields=['is_peer_dm'], name='pa_conv_peer_dm_idx'),
        ),
        migrations.AddIndex(
            model_name='agentpeerlink',
            index=models.Index(fields=['pair_key'], name='agent_peer_pair_key'),
        ),
        migrations.AddIndex(
            model_name='agentpeerlink',
            index=models.Index(fields=['is_enabled'], name='agent_peer_enabled_idx'),
        ),
        migrations.AddIndex(
            model_name='agentcommpeerstate',
            index=models.Index(fields=['link', 'channel'], name='agent_peer_state_idx'),
        ),
        migrations.AddIndex(
            model_name='agentcommpeerstate',
            index=models.Index(fields=['window_reset_at'], name='agent_peer_reset_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='agentcommpeerstate',
            unique_together={('link', 'channel')},
        ),
    ]
