# Generated by Django 5.2.16 on 2026-07-27 22:07

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


class Migration(migrations.Migration):

    dependencies = [
        ('keys', '0002_apikeys_active_alter_apikeys_key'),
        ('notifications', '0006_alter_notifications_options_alter_numbers_options_and_more'),
    ]

    operations = [
        migrations.CreateModel(
            name='NotificationMessage',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('channel', models.CharField(choices=[('email', 'Correo'), ('sms', 'SMS'), ('whatsapp', 'WhatsApp')], db_index=True, max_length=12)),
                ('provider', models.CharField(db_index=True, max_length=20)),
                ('status', models.CharField(choices=[('queued', 'En cola'), ('sending', 'Enviando'), ('sent', 'Aceptado por el proveedor'), ('failed', 'Fallido'), ('delivered', 'Entregado'), ('deferred', 'Diferido'), ('bounced', 'Rebotado'), ('dropped', 'Descartado'), ('spam', 'Marcado como spam'), ('read', 'Leido')], db_index=True, default='queued', max_length=20)),
                ('to', models.JSONField(default=list)),
                ('cc', models.JSONField(blank=True, default=list)),
                ('bcc', models.JSONField(blank=True, default=list)),
                ('recipient_count', models.PositiveIntegerField(default=0)),
                ('subject', models.CharField(blank=True, max_length=255)),
                ('sender', models.CharField(blank=True, help_text='Direccion o numero desde el que se envia.', max_length=254)),
                ('sender_name', models.CharField(blank=True, max_length=128)),
                ('reply_to', models.CharField(blank=True, max_length=254)),
                ('template_id', models.CharField(blank=True, max_length=128)),
                ('body_html', models.TextField(blank=True)),
                ('body_text', models.TextField(blank=True)),
                ('attachment_count', models.PositiveSmallIntegerField(default=0)),
                ('attachment_bytes', models.PositiveIntegerField(default=0)),
                ('provider_message_id', models.CharField(blank=True, db_index=True, help_text='Identificador del proveedor; enlaza con los eventos del webhook.', max_length=255)),
                ('attempts', models.PositiveSmallIntegerField(default=0)),
                ('error_code', models.CharField(blank=True, max_length=64)),
                ('error_message', models.TextField(blank=True)),
                ('request_id', models.CharField(blank=True, db_index=True, max_length=64)),
                ('source_ip', models.GenericIPAddressField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('sent_at', models.DateTimeField(blank=True, null=True)),
                ('last_event_at', models.DateTimeField(blank=True, null=True)),
                ('api_key', models.ForeignKey(blank=True, help_text='Se conserva el registro aunque se borre la clave.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='messages', to='keys.apikeys', verbose_name='Clave usada')),
            ],
            options={
                'verbose_name': 'Notificación enviada',
                'verbose_name_plural': 'Notificaciones enviadas',
                'db_table': 'notification_message',
                'ordering': ('-created_at',),
            },
        ),
        migrations.CreateModel(
            name='MessagePayloadBlob',
            fields=[
                ('message', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='payload_blob', serialize=False, to='notifications.notificationmessage')),
                ('data', models.BinaryField(help_text='Contenido serializado en JSON y comprimido con gzip.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
            ],
            options={
                'verbose_name': 'Contenido pendiente',
                'verbose_name_plural': 'Contenidos pendientes',
                'db_table': 'notification_payload_blob',
            },
        ),
        migrations.CreateModel(
            name='NotificationEvent',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('provider', models.CharField(db_index=True, default='sendgrid', max_length=20)),
                ('event', models.CharField(db_index=True, max_length=40)),
                ('recipient', models.CharField(blank=True, help_text='Direccion o numero al que se refiere el evento.', max_length=254)),
                ('provider_message_id', models.CharField(blank=True, db_index=True, max_length=255)),
                ('provider_event_id', models.CharField(help_text='Identificador unico del evento. Garantiza idempotencia.', max_length=128, unique=True)),
                ('occurred_at', models.DateTimeField(db_index=True)),
                ('reason', models.TextField(blank=True)),
                ('status_code', models.CharField(blank=True, max_length=32)),
                ('payload', models.JSONField(default=dict, help_text='Evento completo tal y como lo envio el proveedor.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('message', models.ForeignKey(blank=True, help_text='Envio relacionado. Puede ser nulo si el evento llega sin correlacion.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='events', to='notifications.notificationmessage', verbose_name='Notificación')),
            ],
            options={
                'verbose_name': 'Evento de entrega',
                'verbose_name_plural': 'Eventos de entrega',
                'db_table': 'notification_event',
                'ordering': ('-occurred_at',),
            },
        ),
        migrations.AddIndex(
            model_name='notificationmessage',
            index=models.Index(fields=['channel', '-created_at'], name='notificatio_channel_1434de_idx'),
        ),
        migrations.AddIndex(
            model_name='notificationmessage',
            index=models.Index(fields=['status', '-created_at'], name='notificatio_status_087b9e_idx'),
        ),
        migrations.AddIndex(
            model_name='notificationmessage',
            index=models.Index(fields=['api_key', '-created_at'], name='notificatio_api_key_eafdc2_idx'),
        ),
        migrations.AddIndex(
            model_name='notificationevent',
            index=models.Index(fields=['message', '-occurred_at'], name='notificatio_message_e040dc_idx'),
        ),
        migrations.AddIndex(
            model_name='notificationevent',
            index=models.Index(fields=['event', '-occurred_at'], name='notificatio_event_d4bfd2_idx'),
        ),
    ]
