# Generated by Django 5.2.9 on 2025-12-20 05:12

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="BackupCode",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "code_hash",
                    models.CharField(help_text="SHA256 hash of the backup code", max_length=64),
                ),
                ("is_used", models.BooleanField(db_index=True, default=False)),
                ("used_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="backup_codes",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Backup Code",
                "verbose_name_plural": "Backup Codes",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(fields=["user", "is_used"], name="django_cfg__user_id_6cb01c_idx")
                ],
            },
        ),
        migrations.CreateModel(
            name="TOTPDevice",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        default="Authenticator",
                        help_text="Device name for identification",
                        max_length=100,
                    ),
                ),
                ("secret", models.CharField(help_text="Base32-encoded TOTP secret", max_length=32)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending Confirmation"),
                            ("active", "Active"),
                            ("disabled", "Disabled"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=20,
                    ),
                ),
                (
                    "is_primary",
                    models.BooleanField(
                        default=False, help_text="Primary device used for verification"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "confirmed_at",
                    models.DateTimeField(
                        blank=True, help_text="When device setup was confirmed", null=True
                    ),
                ),
                (
                    "last_used_at",
                    models.DateTimeField(
                        blank=True, help_text="Last successful verification", null=True
                    ),
                ),
                (
                    "last_verified_code",
                    models.CharField(
                        blank=True,
                        help_text="Last verified code to prevent reuse",
                        max_length=10,
                        null=True,
                    ),
                ),
                (
                    "failed_attempts",
                    models.PositiveIntegerField(
                        default=0, help_text="Consecutive failed verification attempts"
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="totp_devices",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "TOTP Device",
                "verbose_name_plural": "TOTP Devices",
                "ordering": ["-is_primary", "-created_at"],
                "indexes": [
                    models.Index(fields=["user", "status"], name="django_cfg__user_id_b540b6_idx"),
                    models.Index(
                        fields=["user", "is_primary"], name="django_cfg__user_id_a168f0_idx"
                    ),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        condition=models.Q(("is_primary", True), ("status", "active")),
                        fields=("user",),
                        name="unique_primary_active_device",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="TwoFactorSession",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending Verification"),
                            ("verified", "Verified"),
                            ("expired", "Expired"),
                            ("failed", "Failed (Max Attempts)"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("ip_address", models.GenericIPAddressField(blank=True, null=True)),
                ("user_agent", models.TextField(blank=True, default="")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "expires_at",
                    models.DateTimeField(db_index=True, help_text="Session validity window"),
                ),
                ("verified_at", models.DateTimeField(blank=True, null=True)),
                (
                    "attempts",
                    models.PositiveIntegerField(
                        default=0, help_text="Failed verification attempts"
                    ),
                ),
                (
                    "max_attempts",
                    models.PositiveIntegerField(
                        default=5, help_text="Lock session after N failures"
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="two_factor_sessions",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Two-Factor Session",
                "verbose_name_plural": "Two-Factor Sessions",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(fields=["user", "status"], name="django_cfg__user_id_99e326_idx"),
                    models.Index(fields=["expires_at"], name="django_cfg__expires_61b84c_idx"),
                ],
            },
        ),
    ]
