# Generated by Django 4.2 on 2026-02-12 07:47

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import drf_sessions.utils.generators
import drf_sessions.validators
import swapper


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        swapper.dependency("drf_sessions", "Session"),
    ]

    operations = [
        migrations.CreateModel(
            name="Session",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "session_id",
                    models.UUIDField(
                        default=drf_sessions.utils.generators.generate_session_id,
                        editable=False,
                        unique=True,
                    ),
                ),
                (
                    "transport",
                    models.CharField(
                        choices=[
                            ("any", "Any"),
                            ("cookie", "Cookie"),
                            ("header", "Header"),
                        ],
                        max_length=6,
                    ),
                ),
                (
                    "context",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        validators=[drf_sessions.validators.validate_context],
                    ),
                ),
                ("revoked_at", models.DateTimeField(blank=True, null=True)),
                (
                    "last_activity_at",
                    models.DateTimeField(default=django.utils.timezone.now),
                ),
                (
                    "absolute_expiry",
                    models.DateTimeField(blank=True, db_index=True, null=True),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="sessions",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Session",
                "verbose_name_plural": "Sessions",
                "ordering": ["-created_at"],
                "abstract": False,
                "swappable": swapper.swappable_setting("drf_sessions", "Session"),
            },
        ),
        migrations.CreateModel(
            name="RefreshToken",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("token_hash", models.CharField(max_length=255, unique=True)),
                ("expires_at", models.DateTimeField()),
                ("consumed_at", models.DateTimeField(blank=True, null=True)),
                (
                    "session",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="refresh_tokens",
                        to=swapper.get_model_name("drf_sessions", "Session"),
                    ),
                ),
            ],
            options={
                "verbose_name": "Refresh Token",
                "verbose_name_plural": "Refresh Tokens",
                "ordering": ["-created_at"],
                "abstract": False,
            },
        ),
        migrations.AddIndex(
            model_name="refreshtoken",
            index=models.Index(
                fields=["session", "created_at"], name="token_session_lookup_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="refreshtoken",
            index=models.Index(
                fields=["token_hash", "consumed_at", "expires_at"],
                name="token_rotation_val_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="session",
            index=models.Index(
                fields=["transport", "created_at"], name="session_audit_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="session",
            index=models.Index(
                fields=["user", "revoked_at", "created_at"], name="session_mgmt_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="session",
            index=models.Index(
                fields=["session_id", "revoked_at", "absolute_expiry"],
                name="session_auth_composite_idx",
            ),
        ),
    ]
