# Generated by Django 5.2 on 2025-06-26 11:33

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0043_remove_old_pa_comms'),
    ]

    operations = [
        migrations.CreateModel(
            name='PersistentAgentCommsEndpoint',
            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'), ('other', 'Other')], max_length=32)),
                ('address', models.CharField(max_length=512)),
                ('is_primary', models.BooleanField(default=False)),
                ('owner_agent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='comms_endpoints', to='api.persistentagent')),
            ],
            options={
                'ordering': ['channel', 'address'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentEmailEndpoint',
            fields=[
                ('endpoint', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='email_meta', serialize=False, to='api.persistentagentcommsendpoint')),
                ('display_name', models.CharField(blank=True, max_length=256)),
                ('verified', models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            name='PersistentAgentSmsEndpoint',
            fields=[
                ('endpoint', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='sms_meta', serialize=False, to='api.persistentagentcommsendpoint')),
                ('carrier_name', models.CharField(blank=True, max_length=128)),
                ('supports_mms', models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            name='PersistentAgentConversation',
            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'), ('other', 'Other')], max_length=32)),
                ('address', models.CharField(max_length=512)),
                ('display_name', models.CharField(blank=True, max_length=256)),
                ('owner_agent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='owned_conversations', to='api.persistentagent')),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentConversationParticipant',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('role', models.CharField(choices=[('agent', 'Agent'), ('human_user', 'Human User'), ('external', 'External')], max_length=16)),
                ('joined_at', models.DateTimeField(auto_now_add=True)),
                ('left_at', models.DateTimeField(blank=True, null=True)),
                ('conversation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='participants', to='api.persistentagentconversation')),
                ('endpoint', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='conversation_memberships', to='api.persistentagentcommsendpoint')),
            ],
        ),
        migrations.CreateModel(
            name='PersistentAgentMessage',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('seq', models.BigIntegerField(editable=False, unique=True)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('is_outbound', models.BooleanField()),
                ('body', models.TextField()),
                ('raw_payload', models.JSONField(blank=True, default=dict)),
                ('conversation', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='messages', to='api.persistentagentconversation')),
                ('from_endpoint', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='messages_sent', to='api.persistentagentcommsendpoint')),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='replies', to='api.persistentagentmessage')),
                ('to_endpoint', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='messages_received', to='api.persistentagentcommsendpoint')),
            ],
            options={
                'ordering': ['-seq'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentMessageAttachment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('file', models.FileField(upload_to='agent_attachments/%Y/%m/%d/')),
                ('content_type', models.CharField(max_length=128)),
                ('file_size', models.PositiveBigIntegerField()),
                ('filename', models.CharField(max_length=512)),
                ('message', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='attachments', to='api.persistentagentmessage')),
            ],
        ),
        migrations.AddIndex(
            model_name='persistentagentcommsendpoint',
            index=models.Index(fields=['owner_agent', 'channel'], name='pa_ep_agent_channel_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='persistentagentcommsendpoint',
            unique_together={('channel', 'address')},
        ),
        migrations.AddIndex(
            model_name='persistentagentconversation',
            index=models.Index(fields=['channel', 'address'], name='pa_conv_channel_addr_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentconversationparticipant',
            index=models.Index(fields=['endpoint', 'conversation'], name='pa_part_ep_conv_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='persistentagentconversationparticipant',
            unique_together={('conversation', 'endpoint')},
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['conversation', '-seq'], name='pa_msg_conv_seq_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['conversation', '-timestamp'], name='pa_msg_conv_ts_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['from_endpoint', 'to_endpoint', '-seq'], name='pa_msg_endpoints_seq_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['from_endpoint', '-timestamp'], name='pa_msg_from_ts_idx'),
        ),
    ]
