# Generated by Django 5.2 on 2026-04-14 00:00

from django.db import migrations, models


def _default_daily_credit_target(plan_identifier: str | None) -> int:
    normalized = (plan_identifier or "free").lower()
    if normalized in {"startup", "pln_l_m_v1", "scale", "org_team"}:
        return 10
    return 5


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

    for config in DailyCreditConfig.objects.select_related("plan_version__plan").all():
        plan_identifier = config.plan_name
        if not plan_identifier and getattr(config, "plan_version_id", None):
            plan_version = config.plan_version
            plan_identifier = getattr(plan_version, "legacy_plan_code", None)
            if not plan_identifier:
                plan = getattr(plan_version, "plan", None)
                plan_identifier = getattr(plan, "slug", None)

        config.default_daily_credit_target = _default_daily_credit_target(plan_identifier)
        config.save(update_fields=["default_daily_credit_target"])


class Migration(migrations.Migration):

    dependencies = [
        ("api", "0350_systemskillprofile_systemskillprofilesecret_and_more"),
    ]

    operations = [
        migrations.AddField(
            model_name="dailycreditconfig",
            name="default_daily_credit_target",
            field=models.PositiveIntegerField(
                blank=True,
                help_text="Base soft daily credit target applied to new agents before intelligence-tier scaling.",
                null=True,
            ),
        ),
        migrations.RunPython(populate_default_daily_credit_targets, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="dailycreditconfig",
            name="default_daily_credit_target",
            field=models.PositiveIntegerField(
                help_text="Base soft daily credit target applied to new agents before intelligence-tier scaling.",
            ),
        ),
    ]
