# Generated by Django 5.2 on 2025-12-11 00:00
from decimal import Decimal
from django.db import migrations, models
from django.db.models import Max


def seed_daily_credit_configs(apps, schema_editor):
    DailyCreditConfig = apps.get_model("api", "DailyCreditConfig")

    plan_names = ("free", "startup", "pln_l_m_v1", "org_team")
    defaults = {
        "slider_min": Decimal("0"),
        "slider_max": Decimal("50"),
        "slider_step": Decimal("1"),
        "burn_rate_threshold_per_hour": Decimal("3"),
        "burn_rate_window_minutes": 60,
        "hard_limit_multiplier": Decimal("2"),
    }

    existing_configs = {cfg.plan_name: cfg for cfg in DailyCreditConfig.objects.all()}

    max_id = DailyCreditConfig.objects.aggregate(max_id=Max("singleton_id")).get("max_id") or 0
    next_id = int(max_id) + 1

    for plan_name in plan_names:
        if plan_name in existing_configs:
            continue

        DailyCreditConfig.objects.create(
            plan_name=plan_name,
            singleton_id=next_id,
            slider_min=defaults["slider_min"],
            slider_max=defaults["slider_max"],
            slider_step=defaults["slider_step"],
            burn_rate_threshold_per_hour=defaults["burn_rate_threshold_per_hour"],
            burn_rate_window_minutes=defaults["burn_rate_window_minutes"],
            hard_limit_multiplier=defaults["hard_limit_multiplier"],
        )
        next_id += 1


class Migration(migrations.Migration):

    dependencies = [
        ("api", "0226_toolconfig_search_web_result_count"),
    ]

    operations = [
        migrations.AddField(
            model_name="dailycreditconfig",
            name="plan_name",
            field=models.CharField(
                choices=[
                    ("free", "Free"),
                    ("startup", "Startup"),
                    ("pln_l_m_v1", "Scale"),
                    ("org_team", "Team"),
                ],
                default="free",
                help_text="Plan identifier the daily credit pacing settings apply to.",
                max_length=32,
            ),
            preserve_default=False,
        ),
        migrations.RunPython(seed_daily_credit_configs, migrations.RunPython.noop),
        migrations.RemoveField(
            model_name="dailycreditconfig",
            name="singleton_id",
        ),
        migrations.AlterField(
            model_name="dailycreditconfig",
            name="plan_name",
            field=models.CharField(
                choices=[
                    ("free", "Free"),
                    ("startup", "Startup"),
                    ("pln_l_m_v1", "Scale"),
                    ("org_team", "Team"),
                ],
                help_text="Plan identifier the daily credit pacing settings apply to.",
                max_length=32,
                primary_key=True,
                serialize=False,
            ),
        ),
        migrations.AlterModelOptions(
            name="dailycreditconfig",
            options={
                "ordering": ["plan_name"],
                "verbose_name": "Daily credit pacing configuration",
                "verbose_name_plural": "Daily credit pacing configuration",
            },
        ),
    ]
