# Generated by Django 5.2.15 on 2026-07-15 22:35

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('customers', '0001_initial'),
        ('tenancy', '0002_auditlog'),
    ]

    operations = [
        migrations.CreateModel(
            name='CommunicationAutomation',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=150)),
                ('trigger', models.CharField(choices=[('CUSTOMER_CREATED', 'Customer Created'), ('INVOICE_GENERATED', 'Invoice Generated'), ('PAYMENT_VERIFIED', 'Payment Verified'), ('COMPLAINT_CREATED', 'Complaint Created'), ('COMPLAINT_CLOSED', 'Complaint Closed'), ('SERVICE_SUSPENDED', 'Service Suspended'), ('SERVICE_RESTORED', 'Service Restored')], max_length=50)),
                ('execution_order', models.PositiveIntegerField(default=1)),
                ('is_enabled', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'communications_automation',
                'ordering': ['name'],
            },
        ),
        migrations.CreateModel(
            name='CommunicationProvider',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=150)),
                ('provider_type', models.CharField(choices=[('WHATSAPP', 'WhatsApp'), ('SMS', 'SMS'), ('EMAIL', 'Email')], max_length=20)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active'), ('INACTIVE', 'Inactive')], default='ACTIVE', max_length=20)),
                ('business_id', models.CharField(blank=True, max_length=255)),
                ('phone_number_id', models.CharField(blank=True, max_length=255)),
                ('access_token', models.TextField(blank=True)),
                ('webhook_verify_token', models.CharField(blank=True, max_length=255)),
                ('api_url', models.URLField(blank=True)),
                ('sender_id', models.CharField(blank=True, max_length=100)),
                ('is_default', models.BooleanField(default=False)),
                ('last_health_check', models.DateTimeField(blank=True, null=True)),
                ('is_connected', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'communications_provider',
                'ordering': ['name'],
            },
        ),
        migrations.CreateModel(
            name='CommunicationQueue',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('recipient', models.CharField(max_length=50)),
                ('payload', models.JSONField(blank=True, default=dict)),
                ('status', models.CharField(choices=[('PENDING', 'Pending'), ('PROCESSING', 'Processing'), ('SENT', 'Sent'), ('FAILED', 'Failed')], default='PENDING', max_length=20)),
                ('scheduled_at', models.DateTimeField()),
                ('sent_at', models.DateTimeField(blank=True, null=True)),
                ('retry_count', models.PositiveIntegerField(default=0)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='communication_queue', to='customers.customer')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('provider', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='queue_items', to='communications.communicationprovider')),
            ],
            options={
                'db_table': 'communications_queue',
                'ordering': ['scheduled_at'],
            },
        ),
        migrations.CreateModel(
            name='CommunicationLog',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('recipient', models.CharField(max_length=50)),
                ('status', models.CharField(choices=[('DELIVERED', 'Delivered'), ('FAILED', 'Failed'), ('PENDING', 'Pending')], default='PENDING', max_length=20)),
                ('provider_response', models.TextField(blank=True)),
                ('provider_response_code', models.CharField(blank=True, max_length=100)),
                ('error_message', models.TextField(blank=True)),
                ('delivered_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('queue', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='log', to='communications.communicationqueue')),
            ],
            options={
                'db_table': 'communications_log',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='CommunicationSchedule',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('frequency', models.CharField(choices=[('DAILY', 'Daily'), ('WEEKLY', 'Weekly'), ('MONTHLY', 'Monthly'), ('ONCE', 'One Time')], max_length=20)),
                ('next_run', models.DateTimeField()),
                ('last_run', models.DateTimeField(blank=True, null=True)),
                ('is_enabled', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('automation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='schedules', to='communications.communicationautomation')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'communications_schedule',
                'ordering': ['next_run'],
            },
        ),
        migrations.CreateModel(
            name='CommunicationTemplate',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=150)),
                ('subject', models.CharField(blank=True, max_length=255)),
                ('body', models.TextField()),
                ('variables', models.JSONField(blank=True, default=list)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active'), ('DRAFT', 'Draft'), ('ARCHIVED', 'Archived')], default='ACTIVE', max_length=20)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('communication_provider', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='templates', to='communications.communicationprovider')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'communications_template',
                'ordering': ['name'],
            },
        ),
        migrations.AddField(
            model_name='communicationqueue',
            name='template',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='queue_items', to='communications.communicationtemplate'),
        ),
        migrations.AddField(
            model_name='communicationautomation',
            name='template',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='automations', to='communications.communicationtemplate'),
        ),
        migrations.AddIndex(
            model_name='communicationprovider',
            index=models.Index(fields=['organization', 'provider_type'], name='comm_provider_type_idx'),
        ),
        migrations.AddIndex(
            model_name='communicationprovider',
            index=models.Index(fields=['organization', 'status'], name='comm_provider_status_idx'),
        ),
        migrations.AddConstraint(
            model_name='communicationprovider',
            constraint=models.UniqueConstraint(fields=('organization', 'name'), name='unique_provider_name_per_org'),
        ),
        migrations.AddIndex(
            model_name='communicationtemplate',
            index=models.Index(fields=['organization', 'communication_provider'], name='comm_template_provider_idx'),
        ),
        migrations.AddIndex(
            model_name='communicationtemplate',
            index=models.Index(fields=['organization', 'status'], name='comm_template_status_idx'),
        ),
        migrations.AddConstraint(
            model_name='communicationtemplate',
            constraint=models.UniqueConstraint(fields=('organization', 'name'), name='unique_template_name_per_org'),
        ),
        migrations.AddConstraint(
            model_name='communicationautomation',
            constraint=models.UniqueConstraint(fields=('organization', 'name'), name='unique_automation_name_per_org'),
        ),
    ]
