# Generated by Django 5.2.9 on 2025-12-29 15:36

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


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="AIProviderConfig",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        db_index=True, help_text="配置名称（唯一标识）", max_length=50, unique=True
                    ),
                ),
                (
                    "provider_type",
                    models.CharField(
                        choices=[
                            ("openai", "OpenAI"),
                            ("deepseek", "DeepSeek"),
                            ("qwen", "通义千问"),
                            ("moonshot", "Moonshot"),
                            ("custom", "自定义"),
                        ],
                        help_text="提供商类型",
                        max_length=20,
                    ),
                ),
                ("is_active", models.BooleanField(default=True, help_text="是否启用")),
                (
                    "priority",
                    models.IntegerField(
                        db_index=True,
                        default=10,
                        help_text="优先级（数字越小越优先，用于failover）",
                    ),
                ),
                (
                    "base_url",
                    models.URLField(
                        help_text="API Base URL（如 https://api.openai.com/v1）", max_length=500
                    ),
                ),
                ("api_key", models.CharField(help_text="API Key", max_length=500)),
                (
                    "default_model",
                    models.CharField(
                        default="gpt-3.5-turbo", help_text="默认模型名称", max_length=50
                    ),
                ),
                (
                    "daily_budget_limit",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="每日预算限制（美元）",
                        max_digits=10,
                        null=True,
                    ),
                ),
                (
                    "monthly_budget_limit",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="每月预算限制（美元）",
                        max_digits=12,
                        null=True,
                    ),
                ),
                (
                    "extra_config",
                    models.JSONField(blank=True, default=dict, help_text="额外配置参数"),
                ),
                ("description", models.TextField(blank=True, help_text="描述")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "last_used_at",
                    models.DateTimeField(blank=True, help_text="最后使用时间", null=True),
                ),
            ],
            options={
                "verbose_name": "AI提供商配置",
                "verbose_name_plural": "AI提供商配置",
                "db_table": "ai_provider_config",
                "ordering": ["priority", "name"],
                "indexes": [
                    models.Index(
                        fields=["provider_type", "is_active"], name="ai_provider_provide_3ad5e2_idx"
                    ),
                    models.Index(
                        fields=["is_active", "priority"], name="ai_provider_is_acti_63f7fc_idx"
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="AIUsageLog",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("model", models.CharField(db_index=True, help_text="使用的模型", max_length=50)),
                (
                    "request_type",
                    models.CharField(
                        default="chat",
                        help_text="请求类型（chat/completion/embedding等）",
                        max_length=20,
                    ),
                ),
                ("prompt_tokens", models.IntegerField(default=0, help_text="输入token数量")),
                ("completion_tokens", models.IntegerField(default=0, help_text="输出token数量")),
                (
                    "total_tokens",
                    models.IntegerField(db_index=True, default=0, help_text="总token数量"),
                ),
                ("response_time_ms", models.IntegerField(help_text="响应时间（毫秒）")),
                (
                    "estimated_cost",
                    models.DecimalField(
                        db_index=True, decimal_places=6, help_text="预估成本（美元）", max_digits=10
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("success", "成功"),
                            ("error", "错误"),
                            ("timeout", "超时"),
                            ("rate_limited", "限流"),
                        ],
                        db_index=True,
                        help_text="调用状态",
                        max_length=20,
                    ),
                ),
                ("error_message", models.TextField(blank=True, help_text="错误信息")),
                (
                    "request_metadata",
                    models.JSONField(blank=True, default=dict, help_text="请求元数据"),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="创建时间"),
                ),
                (
                    "provider",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="usage_logs",
                        to="ai_provider.aiproviderconfig",
                    ),
                ),
            ],
            options={
                "verbose_name": "AI调用日志",
                "verbose_name_plural": "AI调用日志",
                "db_table": "ai_usage_log",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["provider", "-created_at"], name="ai_usage_lo_provide_ad3915_idx"
                    ),
                    models.Index(
                        fields=["model", "-created_at"], name="ai_usage_lo_model_e96a50_idx"
                    ),
                    models.Index(
                        fields=["status", "-created_at"], name="ai_usage_lo_status_bbcb0f_idx"
                    ),
                    models.Index(fields=["-created_at"], name="ai_usage_lo_created_c86018_idx"),
                ],
            },
        ),
    ]
