# Generated by Django 5.1.3 on 2026-05-25 07:48

import django.db.models.deletion
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("notifications", "0002_notificationpreference_notification_attempt_and_more"),
        ("tenants", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="SmsProvider",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                ("name", models.CharField(max_length=128, verbose_name="name")),
                (
                    "slug",
                    models.SlugField(
                        help_text="Machine-readable identifier, e.g. 'sms_ir' or 'twilio'",
                        max_length=64,
                        unique=True,
                        verbose_name="slug",
                    ),
                ),
                ("description", models.TextField(blank=True, verbose_name="description")),
                (
                    "is_active",
                    models.BooleanField(db_index=True, default=True, verbose_name="is active"),
                ),
                (
                    "credentials",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text='JSON object with provider credentials. Example: {"api_key": "xxx", "line_number": "30004505001175"}',
                        verbose_name="credentials",
                    ),
                ),
                (
                    "extra_params",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text='JSON object with provider-specific parameters. Example: {"otp_template_id": 453080, "otp_param_name": "Code"}',
                        verbose_name="extra parameters",
                    ),
                ),
                (
                    "supported_country_codes",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='List of E.164 dial codes this provider handles. Example: ["+98"] for Iran only. Empty list = all countries.',
                        verbose_name="supported country codes",
                    ),
                ),
                (
                    "priority",
                    models.PositiveIntegerField(
                        default=0,
                        help_text="Higher value = preferred when multiple providers match the same country.",
                        verbose_name="priority",
                    ),
                ),
            ],
            options={
                "verbose_name": "SMS provider",
                "verbose_name_plural": "SMS providers",
                "ordering": ["-priority", "name"],
            },
        ),
        migrations.CreateModel(
            name="TenantSmsRateLimit",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                (
                    "max_per_minute",
                    models.PositiveIntegerField(
                        default=10,
                        help_text="Maximum SMS allowed per minute per tenant. 0 = unlimited.",
                        verbose_name="max per minute",
                    ),
                ),
                (
                    "max_per_hour",
                    models.PositiveIntegerField(
                        default=100,
                        help_text="Maximum SMS allowed per hour per tenant. 0 = unlimited.",
                        verbose_name="max per hour",
                    ),
                ),
                (
                    "max_per_day",
                    models.PositiveIntegerField(
                        default=1000,
                        help_text="Maximum SMS allowed per day per tenant. 0 = unlimited.",
                        verbose_name="max per day",
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True,
                        help_text="Disable to bypass rate limits for this tenant (use with caution).",
                        verbose_name="rate limiting active",
                    ),
                ),
                (
                    "tenant",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="sms_rate_limit",
                        to="tenants.tenant",
                        verbose_name="tenant",
                    ),
                ),
            ],
            options={
                "verbose_name": "tenant SMS rate limit",
                "verbose_name_plural": "tenant SMS rate limits",
            },
        ),
        migrations.CreateModel(
            name="SmsDeliveryLog",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                (
                    "mobile",
                    models.CharField(db_index=True, max_length=32, verbose_name="mobile number"),
                ),
                ("message_text", models.TextField(blank=True, verbose_name="message text")),
                (
                    "template_id",
                    models.CharField(blank=True, max_length=64, verbose_name="template ID"),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("sent", "Sent"),
                            ("delivered", "Delivered"),
                            ("failed", "Failed"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=16,
                        verbose_name="status",
                    ),
                ),
                (
                    "provider_message_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        max_length=128,
                        verbose_name="provider message ID",
                    ),
                ),
                (
                    "cost",
                    models.DecimalField(
                        blank=True, decimal_places=4, max_digits=10, null=True, verbose_name="cost"
                    ),
                ),
                ("error_message", models.TextField(blank=True, verbose_name="error message")),
                (
                    "raw_response",
                    models.JSONField(blank=True, default=dict, verbose_name="raw response"),
                ),
                (
                    "tenant",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="sms_delivery_logs",
                        to="tenants.tenant",
                        verbose_name="tenant",
                    ),
                ),
                (
                    "provider",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="delivery_logs",
                        to="notifications.smsprovider",
                        verbose_name="provider",
                    ),
                ),
            ],
            options={
                "verbose_name": "SMS delivery log",
                "verbose_name_plural": "SMS delivery logs",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["tenant", "-created_at"], name="notificatio_tenant__2f3f54_idx"
                    ),
                    models.Index(
                        fields=["provider", "status", "-created_at"],
                        name="notificatio_provide_f6ed3e_idx",
                    ),
                    models.Index(
                        fields=["mobile", "-created_at"], name="notificatio_mobile_e41ba1_idx"
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="SmsProviderTemplate",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Internal label, e.g. 'otp', 'invoice_notification'",
                        max_length=128,
                        verbose_name="name",
                    ),
                ),
                (
                    "provider_template_id",
                    models.CharField(
                        help_text="Template ID as defined on the provider's platform",
                        max_length=64,
                        verbose_name="provider template ID",
                    ),
                ),
                (
                    "params_schema",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='List of parameter names required by this template. Example: ["Code"]',
                        verbose_name="parameters schema",
                    ),
                ),
                (
                    "is_default_otp",
                    models.BooleanField(
                        default=False,
                        help_text="Mark this as the default template for OTP sends on this provider.",
                        verbose_name="is default OTP template",
                    ),
                ),
                (
                    "provider",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="templates",
                        to="notifications.smsprovider",
                        verbose_name="provider",
                    ),
                ),
            ],
            options={
                "verbose_name": "SMS provider template",
                "verbose_name_plural": "SMS provider templates",
                "constraints": [
                    models.UniqueConstraint(
                        fields=("provider", "name"), name="uniq_sms_provider_template_name"
                    )
                ],
            },
        ),
    ]
