"""Initial migration for Django Smart Ratelimit."""

# Generated by Django Smart Ratelimit

import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
    """Initial migration creating rate limit tables."""

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="RateLimitEntry",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "key",
                    models.CharField(
                        db_index=True,
                        help_text=(
                            "The rate limit key (e.g., 'user:123', 'ip:192.168.1.1')"
                        ),
                        max_length=255,
                    ),
                ),
                (
                    "timestamp",
                    models.DateTimeField(
                        db_index=True,
                        default=django.utils.timezone.now,
                        help_text="When this rate limit entry was created",
                    ),
                ),
                (
                    "expires_at",
                    models.DateTimeField(
                        db_index=True, help_text="When this rate limit entry expires"
                    ),
                ),
                (
                    "algorithm",
                    models.CharField(
                        choices=[
                            ("sliding_window", "Sliding Window"),
                            ("fixed_window", "Fixed Window"),
                        ],
                        default="sliding_window",
                        help_text="The algorithm used for this rate limit",
                        max_length=50,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "Rate Limit Entry",
                "verbose_name_plural": "Rate Limit Entries",
                "db_table": "django_smart_ratelimit_entry",
                "ordering": ["-timestamp"],
            },
        ),
        migrations.CreateModel(
            name="RateLimitCounter",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "key",
                    models.CharField(
                        db_index=True,
                        help_text=(
                            "The rate limit key (e.g., 'user:123', 'ip:192.168.1.1')"
                        ),
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "count",
                    models.PositiveIntegerField(
                        default=0, help_text="Current count for this rate limit key"
                    ),
                ),
                (
                    "window_start",
                    models.DateTimeField(
                        help_text="Start of the current rate limit window"
                    ),
                ),
                (
                    "window_end",
                    models.DateTimeField(
                        help_text="End of the current rate limit window"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "Rate Limit Counter",
                "verbose_name_plural": "Rate Limit Counters",
                "db_table": "django_smart_ratelimit_counter",
            },
        ),
        migrations.CreateModel(
            name="RateLimitConfig",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "key_pattern",
                    models.CharField(
                        db_index=True,
                        help_text=(
                            "Pattern to match rate limit keys (supports wildcards)"
                        ),
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "rate_limit",
                    models.CharField(
                        help_text="Rate limit (e.g., '100/h', '10/m')", max_length=50
                    ),
                ),
                (
                    "algorithm",
                    models.CharField(
                        choices=[
                            ("sliding_window", "Sliding Window"),
                            ("fixed_window", "Fixed Window"),
                        ],
                        default="sliding_window",
                        help_text="Algorithm to use for this rate limit",
                        max_length=50,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True, help_text="Whether this configuration is active"
                    ),
                ),
                (
                    "description",
                    models.TextField(
                        blank=True,
                        help_text="Description of this rate limit configuration",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "Rate Limit Configuration",
                "verbose_name_plural": "Rate Limit Configurations",
                "db_table": "django_smart_ratelimit_config",
                "ordering": ["-created_at"],
            },
        ),
        migrations.AddIndex(
            model_name="ratelimitentry",
            index=models.Index(
                fields=["key", "expires_at"], name="django_smar_key_8ff7e9_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="ratelimitentry",
            index=models.Index(
                fields=["expires_at"], name="django_smar_expires_f8e4cc_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="ratelimitentry",
            index=models.Index(
                fields=["key", "algorithm"], name="django_smar_key_b42c8a_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="ratelimitcounter",
            index=models.Index(
                fields=["key", "window_end"], name="django_smar_key_24c4a0_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="ratelimitcounter",
            index=models.Index(
                fields=["window_end"], name="django_smar_window__3e0e6a_idx"
            ),
        ),
    ]
