# Generated by Django 5.2.10 on 2026-03-10 06:18

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


class Migration(migrations.Migration):

    dependencies = [
        ("account", "0017_systemsettingsmodel_backup_mail_runtime"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name="accountprofilemodel",
            name="mcp_enabled",
            field=models.BooleanField(
                default=True,
                help_text="关闭后，该用户所有 MCP/SDK Token 将立即失效",
                verbose_name="允许 MCP/SDK 访问",
            ),
        ),
        migrations.AddField(
            model_name="systemsettingsmodel",
            name="allow_token_plaintext_view",
            field=models.BooleanField(
                default=True,
                help_text="关闭后，生成后不再显示完整 Token，历史 Token 也不可明文查看",
                verbose_name="允许查看 Token 明文",
            ),
        ),
        migrations.AddField(
            model_name="systemsettingsmodel",
            name="default_mcp_enabled",
            field=models.BooleanField(
                default=True,
                help_text="新注册或新批准用户默认是否允许 MCP/SDK 访问，由管理员决定",
                verbose_name="新用户默认开启 MCP/SDK",
            ),
        ),
        migrations.CreateModel(
            name="UserAccessTokenModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        default="default",
                        help_text="例如：Claude Desktop / Local SDK / VPS Script",
                        max_length=100,
                        verbose_name="Token名称",
                    ),
                ),
                (
                    "key",
                    models.CharField(
                        db_index=True, max_length=64, unique=True, verbose_name="Token Key"
                    ),
                ),
                (
                    "key_encrypted",
                    models.TextField(
                        blank=True,
                        help_text="用于按系统配置决定是否允许明文查看",
                        verbose_name="Token密文",
                    ),
                ),
                (
                    "last_used_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="最后使用时间"),
                ),
                (
                    "revoked_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="撤销时间"),
                ),
                ("is_active", models.BooleanField(default=True, verbose_name="是否有效")),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_access_tokens",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="创建人",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="access_tokens",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="所属用户",
                    ),
                ),
            ],
            options={
                "verbose_name": "用户访问Token",
                "verbose_name_plural": "用户访问Token",
                "db_table": "user_access_token",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["user", "is_active"], name="user_access_user_id_641936_idx"
                    ),
                    models.Index(fields=["key"], name="user_access_key_209554_idx"),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        condition=models.Q(("is_active", True)),
                        fields=("user", "name"),
                        name="uniq_active_access_token_name_per_user",
                    )
                ],
            },
        ),
    ]
