# Generated by Django 6.0.1 on 2026-01-24

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="MCPToken",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="A descriptive name for this token (e.g., 'Production API', 'Dev Testing')",
                        max_length=200,
                    ),
                ),
                (
                    "token_key",
                    models.CharField(
                        db_index=True,
                        editable=False,
                        help_text="Public token key for O(1) database lookup",
                        max_length=16,
                        unique=True,
                    ),
                ),
                (
                    "token_hash",
                    models.CharField(
                        blank=True,
                        editable=False,
                        help_text="SHA-256 hash of the secret portion of the token",
                        max_length=64,
                        null=True,
                        unique=True,
                    ),
                ),
                (
                    "salt",
                    models.CharField(
                        editable=False,
                        help_text="Salt used for hashing the token secret",
                        max_length=32,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True, help_text="Whether this token is currently active"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "expires_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Token expiration date (leave empty for indefinite tokens)",
                        null=True,
                    ),
                ),
                (
                    "last_used_at",
                    models.DateTimeField(
                        blank=True, help_text="Last time this token was used", null=True
                    ),
                ),
                (
                    "groups",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Groups this token belongs to for permission management",
                        related_name="mcp_tokens",
                        to="auth.group",
                    ),
                ),
                (
                    "permissions",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Specific permissions granted to this token",
                        related_name="mcp_tokens",
                        to="auth.permission",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="User for audit logging (actions are logged under this user)",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="mcp_tokens",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "MCP Token",
                "verbose_name_plural": "MCP Tokens",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["is_active", "token_key"], name="mcp_token_active_key_idx"
                    ),
                ],
            },
        ),
    ]
