# Generated by Django 5.2 on 2025-08-21 15:19

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0096_taskcredit_org_ownership'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='CommsAllowlistEntry',
            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(help_text='Email address or E.164 phone number', max_length=512)),
                ('is_active', models.BooleanField(default=True)),
                ('verified', models.BooleanField(default=True, help_text='Reserved for future use. Manual verification flag; currently not enforced.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'ordering': ['channel', 'address'],
            },
        ),
        migrations.RemoveConstraint(
            model_name='taskcredit',
            name='uniq_free_plan_block_per_month_org',
        ),
        migrations.RemoveConstraint(
            model_name='taskcredit',
            name='taskcredit_owner_xor_user_org',
        ),
        migrations.AddField(
            model_name='persistentagent',
            name='whitelist_policy',
            field=models.CharField(choices=[('default', 'Default (Owner or Org Members)'), ('manual', 'Manual Allowlist')], default='default', help_text="Controls who can message this agent and who the agent may contact. Default: user-owned → owner email + verified phone; org-owned → org members' emails + verified phones. Manual: only addresses/numbers listed on the agent's manual allowlist.", max_length=16),
        ),
        migrations.AddConstraint(
            model_name='taskcredit',
            constraint=models.UniqueConstraint(condition=models.Q(('additional_task', False), ('grant_type', 'Plan'), ('plan', 'free'), ('voided', False)), fields=('organization', 'plan', 'grant_month'), name='uniq_free_plan_block_per_month_org'),
        ),
        migrations.AddConstraint(
            model_name='taskcredit',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('organization__isnull', True), ('user__isnull', False)), models.Q(('organization__isnull', False), ('user__isnull', True)), _connector='OR'), name='taskcredit_owner_xor_user_org'),
        ),
        migrations.AddField(
            model_name='commsallowlistentry',
            name='agent',
            field=models.ForeignKey(help_text='Agent to which this allowlist entry applies', on_delete=django.db.models.deletion.CASCADE, related_name='manual_allowlist', to='api.persistentagent'),
        ),
        migrations.AddIndex(
            model_name='commsallowlistentry',
            index=models.Index(fields=['agent', 'channel'], name='allow_agent_channel_idx'),
        ),
        migrations.AddConstraint(
            model_name='commsallowlistentry',
            constraint=models.UniqueConstraint(fields=('agent', 'channel', 'address'), name='uniq_allowlist_agent_channel_address'),
        ),
    ]
