# Generated by Django 5.2.12 on 2026-04-09 15:35

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


class Migration(migrations.Migration):

    dependencies = [
        ("ai_provider", "0003_add_api_key_encrypted_field"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="AIUserFallbackQuota",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "daily_limit",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="每日系统兜底额度（美元）",
                        max_digits=10,
                        null=True,
                    ),
                ),
                (
                    "monthly_limit",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="每月系统兜底额度（美元）",
                        max_digits=12,
                        null=True,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(default=True, help_text="是否启用用户系统兜底额度"),
                ),
                ("admin_note", models.TextField(blank=True, help_text="管理员备注")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "AI 用户兜底额度",
                "verbose_name_plural": "AI 用户兜底额度",
                "db_table": "ai_user_fallback_quota",
            },
        ),
        migrations.AddField(
            model_name="aiproviderconfig",
            name="owner_user",
            field=models.ForeignKey(
                blank=True,
                help_text="当 scope=user 时对应的拥有者",
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="ai_provider_configs",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="aiproviderconfig",
            name="scope",
            field=models.CharField(
                choices=[("system", "System"), ("user", "User")],
                db_index=True,
                default="system",
                help_text="配置归属范围（system/user）",
                max_length=20,
            ),
        ),
        migrations.AddField(
            model_name="aiusagelog",
            name="provider_scope",
            field=models.CharField(
                choices=[
                    ("system_global", "System Global"),
                    ("system_fallback", "System Fallback"),
                    ("personal", "Personal"),
                ],
                db_index=True,
                default="system_global",
                help_text="命中的 provider 归属范围",
                max_length=20,
            ),
        ),
        migrations.AddField(
            model_name="aiusagelog",
            name="quota_charged",
            field=models.BooleanField(default=False, help_text="是否计入用户系统兜底额度"),
        ),
        migrations.AddField(
            model_name="aiusagelog",
            name="user",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="ai_usage_logs",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AlterField(
            model_name="aiproviderconfig",
            name="name",
            field=models.CharField(db_index=True, help_text="配置名称（唯一标识）", max_length=50),
        ),
        migrations.AddIndex(
            model_name="aiproviderconfig",
            index=models.Index(
                fields=["scope", "owner_user", "is_active"], name="ai_provider_scope_3023ac_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="aiusagelog",
            index=models.Index(
                fields=["user", "-created_at"], name="ai_usage_lo_user_id_a51ad9_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="aiusagelog",
            index=models.Index(
                fields=["provider_scope", "-created_at"], name="ai_usage_lo_provide_e81e6f_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="aiproviderconfig",
            constraint=models.UniqueConstraint(
                condition=models.Q(("scope", "user")),
                fields=("owner_user", "name"),
                name="ai_provider_user_name_unique",
            ),
        ),
        migrations.AddConstraint(
            model_name="aiproviderconfig",
            constraint=models.UniqueConstraint(
                condition=models.Q(("scope", "system")),
                fields=("name",),
                name="ai_provider_system_name_unique",
            ),
        ),
        migrations.AddField(
            model_name="aiuserfallbackquota",
            name="user",
            field=models.OneToOneField(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="ai_fallback_quota",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddIndex(
            model_name="aiuserfallbackquota",
            index=models.Index(fields=["is_active"], name="ai_user_fal_is_acti_2d0dde_idx"),
        ),
    ]
