# Generated by Django 4.2.28 on 2026-03-03 13:40

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


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("utils", "0018_add_portfolio_notification_fields"),
    ]

    operations = [
        migrations.CreateModel(
            name="NotificationRule",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(max_length=200)),
                ("description", models.TextField(blank=True)),
                ("notification_type", models.CharField(max_length=50)),
                (
                    "condition_type",
                    models.CharField(
                        choices=[
                            ("role", "User Role"),
                            ("branch", "User Branch"),
                            ("portfolio", "Portfolio Assignment"),
                            ("loan_amount", "Loan Amount"),
                            ("priority", "Notification Priority"),
                            ("time", "Time-based"),
                            ("custom", "Custom Condition"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "condition_field",
                    models.CharField(
                        help_text="Field to check (e.g., 'user.role', 'loan.amount')",
                        max_length=100,
                    ),
                ),
                (
                    "operator",
                    models.CharField(
                        choices=[
                            ("equals", "Equals"),
                            ("not_equals", "Not Equals"),
                            ("greater_than", "Greater Than"),
                            ("less_than", "Less Than"),
                            ("contains", "Contains"),
                            ("in", "In List"),
                            ("not_in", "Not In List"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "condition_value",
                    models.TextField(
                        help_text="Value to compare against (JSON for complex values)"
                    ),
                ),
                (
                    "target_roles",
                    models.JSONField(
                        default=list, help_text="Roles to notify when condition is met"
                    ),
                ),
                (
                    "escalation_delay",
                    models.IntegerField(
                        default=0, help_text="Minutes to wait before escalation"
                    ),
                ),
                (
                    "escalation_roles",
                    models.JSONField(
                        default=list,
                        help_text="Roles to escalate to if not acknowledged",
                    ),
                ),
                ("is_active", models.BooleanField(default=True)),
                (
                    "priority",
                    models.IntegerField(
                        default=1,
                        help_text="Rule priority (lower number = higher priority)",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "db_table": "notification_rules",
                "ordering": ["priority", "name"],
            },
        ),
        migrations.RemoveField(
            model_name="notification",
            name="portfolio_snapshot",
        ),
        migrations.CreateModel(
            name="NotificationTemplate",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(max_length=200)),
                ("notification_type", models.CharField(max_length=50)),
                (
                    "channel",
                    models.CharField(
                        choices=[
                            ("in_app", "In-App Notification"),
                            ("email", "Email"),
                            ("sms", "SMS"),
                            ("push", "Push Notification"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "subject_template",
                    models.CharField(
                        blank=True,
                        help_text="Subject template (for email/push)",
                        max_length=200,
                    ),
                ),
                (
                    "title_template",
                    models.CharField(help_text="Title template", max_length=200),
                ),
                (
                    "message_template",
                    models.TextField(help_text="Message template with placeholders"),
                ),
                (
                    "html_template",
                    models.TextField(
                        blank=True, help_text="HTML template for rich content"
                    ),
                ),
                (
                    "variables",
                    models.JSONField(
                        default=dict, help_text="Available template variables"
                    ),
                ),
                ("is_active", models.BooleanField(default=True)),
                ("is_default", models.BooleanField(default=False)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "db_table": "notification_templates",
                "ordering": ["notification_type", "channel", "name"],
                "unique_together": {("notification_type", "channel", "is_default")},
            },
        ),
        migrations.CreateModel(
            name="NotificationEscalation",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("escalated_at", models.DateTimeField(auto_now_add=True)),
                ("acknowledged_at", models.DateTimeField(blank=True, null=True)),
                ("resolved_at", models.DateTimeField(blank=True, null=True)),
                ("escalation_level", models.IntegerField(default=1)),
                ("notes", models.TextField(blank=True)),
                (
                    "escalated_to",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="escalated_notifications",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "original_notification",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="escalations",
                        to="utils.notification",
                    ),
                ),
                (
                    "rule",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="utils.notificationrule",
                    ),
                ),
            ],
            options={
                "db_table": "notification_escalations",
                "ordering": ["-escalated_at"],
            },
        ),
        migrations.CreateModel(
            name="NotificationBatch",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(max_length=200)),
                ("notification_type", models.CharField(max_length=50)),
                (
                    "target_criteria",
                    models.JSONField(help_text="Criteria for selecting recipients"),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("processing", "Processing"),
                            ("completed", "Completed"),
                            ("failed", "Failed"),
                            ("cancelled", "Cancelled"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("total_recipients", models.IntegerField(default=0)),
                ("sent_count", models.IntegerField(default=0)),
                ("failed_count", models.IntegerField(default=0)),
                ("scheduled_at", models.DateTimeField(blank=True, null=True)),
                ("started_at", models.DateTimeField(blank=True, null=True)),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                ("error_log", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "created_by",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="created_notification_batches",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "template",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="utils.notificationtemplate",
                    ),
                ),
            ],
            options={
                "db_table": "notification_batches",
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="NotificationPreference",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("notification_type", models.CharField(max_length=50)),
                (
                    "channel",
                    models.CharField(
                        choices=[
                            ("in_app", "In-App Notification"),
                            ("email", "Email"),
                            ("sms", "SMS"),
                            ("push", "Push Notification"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "frequency",
                    models.CharField(
                        choices=[
                            ("immediate", "Immediate"),
                            ("hourly", "Hourly Digest"),
                            ("daily", "Daily Digest"),
                            ("weekly", "Weekly Digest"),
                            ("never", "Never"),
                        ],
                        default="immediate",
                        max_length=20,
                    ),
                ),
                ("is_enabled", models.BooleanField(default=True)),
                (
                    "quiet_hours_start",
                    models.TimeField(
                        blank=True,
                        help_text="Start of quiet hours (no notifications)",
                        null=True,
                    ),
                ),
                (
                    "quiet_hours_end",
                    models.TimeField(
                        blank=True, help_text="End of quiet hours", null=True
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="notification_preferences",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "db_table": "notification_preferences",
                "unique_together": {("user", "notification_type", "channel")},
            },
        ),
        migrations.CreateModel(
            name="NotificationDelivery",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "channel",
                    models.CharField(
                        choices=[
                            ("in_app", "In-App Notification"),
                            ("email", "Email"),
                            ("sms", "SMS"),
                            ("push", "Push Notification"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("sent", "Sent"),
                            ("delivered", "Delivered"),
                            ("failed", "Failed"),
                            ("bounced", "Bounced"),
                            ("read", "Read"),
                            ("clicked", "Clicked"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("sent_at", models.DateTimeField(blank=True, null=True)),
                ("delivered_at", models.DateTimeField(blank=True, null=True)),
                ("read_at", models.DateTimeField(blank=True, null=True)),
                ("clicked_at", models.DateTimeField(blank=True, null=True)),
                ("failed_at", models.DateTimeField(blank=True, null=True)),
                ("error_message", models.TextField(blank=True)),
                (
                    "external_id",
                    models.CharField(
                        blank=True,
                        help_text="External service message ID",
                        max_length=200,
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        default=dict, help_text="Additional delivery metadata"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "notification",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="deliveries",
                        to="utils.notification",
                    ),
                ),
                (
                    "recipient",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "db_table": "notification_deliveries",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["notification", "channel"],
                        name="notificatio_notific_31045d_idx",
                    ),
                    models.Index(
                        fields=["recipient", "status"],
                        name="notificatio_recipie_0d578c_idx",
                    ),
                    models.Index(
                        fields=["status", "created_at"],
                        name="notificatio_status_7ba7c6_idx",
                    ),
                ],
            },
        ),
    ]
