# Generated by Django 5.2 on 2025-06-20 16:33

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0038_merge_20250619_0111'),
    ]

    operations = [
        migrations.CreateModel(
            name='PersistentAgent',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=255)),
                ('charter', models.TextField()),
                ('schedule', models.CharField(help_text='Cron-like schedule expression', max_length=128, validators=[django.core.validators.RegexValidator(message='Schedule must be a valid cron expression or shorthand like @daily', regex='^(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\\d+(ns|us|µs|ms|s|m|h))+)|([*\\d\\-,/# ]+)$')])),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('browser_use_agent', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='persistent_agent', to='api.browseruseagent')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentEmail',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('address', models.CharField(max_length=320)),
                ('is_primary', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('persistent_agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='emails', to='api.persistentagent')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentSmsNumber',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('phone_number', models.CharField(max_length=32, validators=[django.core.validators.RegexValidator(message='Phone number must be in E.164 format (e.g., +1234567890)', regex='^\\+?[1-9]\\d{1,14}$')])),
                ('is_primary', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('persistent_agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sms_numbers', to='api.persistentagent')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PersistentAgentMessage',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('from_external_address', models.CharField(blank=True, max_length=320, null=True)),
                ('to_external_address', models.CharField(blank=True, max_length=320, null=True)),
                ('channel', models.CharField(choices=[('email', 'Email'), ('sms', 'SMS')], max_length=10)),
                ('content', models.TextField()),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('from_email', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sent_messages', to='api.persistentagentemail')),
                ('persistent_agent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='api.persistentagent')),
                ('to_email', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='received_messages', to='api.persistentagentemail')),
                ('from_sms', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sent_messages', to='api.persistentagentsmsnumber')),
                ('to_sms', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='received_messages', to='api.persistentagentsmsnumber')),
            ],
            options={
                'ordering': ['-timestamp'],
            },
        ),
        migrations.AddIndex(
            model_name='persistentagent',
            index=models.Index(fields=['schedule'], name='pa_schedule_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentemail',
            index=models.Index(fields=['persistent_agent'], name='pa_email_agent_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentsmsnumber',
            index=models.Index(fields=['persistent_agent'], name='pa_sms_agent_idx'),
        ),
        migrations.AddIndex(
            model_name='persistentagentmessage',
            index=models.Index(fields=['persistent_agent', '-timestamp'], name='pa_msg_agent_ts_idx'),
        ),
    ]
