# Generated by Django 5.2.7 on 2026-02-10 12:23

import uuid

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


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Token",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.CharField(max_length=128)),
                ("description", models.TextField(blank=True)),
                (
                    "token_type",
                    models.CharField(
                        choices=[("user", "User"), ("system", "System")],
                        default="user",
                        max_length=10,
                    ),
                ),
                ("key", models.CharField(max_length=256, unique=True)),
                ("prefix", models.CharField(db_index=True, max_length=12)),
                ("hint", models.CharField(max_length=8)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("expires_at", models.DateTimeField(blank=True, null=True)),
                ("last_used_at", models.DateTimeField(blank=True, null=True)),
                ("revoked", models.BooleanField(default=False)),
                ("purged", models.BooleanField(default=False)),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_tokens",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "scopes",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Django permissions associated with this token",
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="api_tokens",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "db_table": "keysmith_token",
                "ordering": ["-created_at"],
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="TokenAuditLog",
            fields=[
                ("id", models.BigAutoField(primary_key=True, serialize=False)),
                (
                    "action",
                    models.CharField(
                        choices=[
                            ("auth_success", "Authentication Success"),
                            ("auth_failed", "Authentication Failed"),
                            ("revoked", "Token Revoked"),
                            ("rotated", "Token Rotated"),
                        ],
                        max_length=64,
                    ),
                ),
                ("path", models.TextField(help_text="Request path (without domain)")),
                ("method", models.CharField(help_text="HTTP method", max_length=10)),
                (
                    "status_code",
                    models.PositiveSmallIntegerField(help_text="HTTP response status code"),
                ),
                ("ip_address", models.GenericIPAddressField(blank=True, null=True)),
                ("user_agent", models.TextField(blank=True, null=True)),
                (
                    "extra",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Additional context (errors, reason, actor, etc.)",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "token",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="audit_logs",
                        to="keysmith.token",
                    ),
                ),
            ],
            options={
                "db_table": "keysmith_token_audit_log",
                "ordering": ["-created_at"],
                "abstract": False,
            },
        ),
        migrations.AddIndex(
            model_name="token",
            index=models.Index(fields=["key"], name="keysmith_to_key_a590d5_idx"),
        ),
        migrations.AddIndex(
            model_name="token",
            index=models.Index(fields=["expires_at"], name="keysmith_to_expires_c899f2_idx"),
        ),
        migrations.AddIndex(
            model_name="token",
            index=models.Index(fields=["revoked"], name="keysmith_to_revoked_7c9b5b_idx"),
        ),
        migrations.AddIndex(
            model_name="tokenauditlog",
            index=models.Index(fields=["token"], name="keysmith_to_token_i_09b1bd_idx"),
        ),
        migrations.AddIndex(
            model_name="tokenauditlog",
            index=models.Index(fields=["action"], name="keysmith_to_action_843630_idx"),
        ),
        migrations.AddIndex(
            model_name="tokenauditlog",
            index=models.Index(fields=["created_at"], name="keysmith_to_created_65d41f_idx"),
        ),
    ]
