# Generated by Django 5.2.11 on 2026-03-02 14:17

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


def _backfill_variant_policies(apps, schema_editor):
    CommandProfile = apps.get_model("core", "CommandProfile")
    CommandAction = apps.get_model("core", "CommandAction")
    CommandVariantPolicy = apps.get_model("core", "CommandVariantPolicy")

    for profile in CommandProfile.objects.all().iterator():
        actions = list(CommandAction.objects.filter(profile=profile))
        post_result_enabled = any(
            str(getattr(row, "action_type", "")) == "post_result"
            and bool(getattr(row, "enabled", False))
            for row in actions
        )
        send_status_to_source = (
            str(getattr(profile, "visibility_mode", "") or "") == "status_in_source"
        )
        if str(getattr(profile, "slug", "") or "") == "bp":
            rows = (
                ("bp", "ai", 0),
                ("bp_set", "verbatim", 1),
                ("bp_set_range", "verbatim", 2),
            )
        else:
            rows = (("default", "verbatim", 0),)

        for key, generation_mode, position in rows:
            CommandVariantPolicy.objects.get_or_create(
                profile=profile,
                variant_key=key,
                defaults={
                    "enabled": True,
                    "generation_mode": generation_mode,
                    "send_plan_to_egress": bool(post_result_enabled),
                    "send_status_to_source": bool(send_status_to_source),
                    "send_status_to_egress": False,
                    "position": int(position),
                },
            )


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0030_chattasksource_settings"),
    ]

    operations = [
        migrations.CreateModel(
            name="CommandVariantPolicy",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("variant_key", models.CharField(default="default", max_length=64)),
                ("enabled", models.BooleanField(default=True)),
                (
                    "generation_mode",
                    models.CharField(
                        choices=[("ai", "AI"), ("verbatim", "Verbatim")],
                        default="verbatim",
                        max_length=32,
                    ),
                ),
                ("send_plan_to_egress", models.BooleanField(default=True)),
                ("send_status_to_source", models.BooleanField(default=True)),
                ("send_status_to_egress", models.BooleanField(default=False)),
                ("position", models.PositiveIntegerField(default=0)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "profile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="variant_policies",
                        to="core.commandprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["position", "id"],
                "indexes": [
                    models.Index(
                        fields=["profile", "enabled", "variant_key"],
                        name="core_comman_profile_7913f5_idx",
                    )
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("profile", "variant_key"),
                        name="unique_command_variant_policy_per_profile",
                    )
                ],
            },
        ),
        migrations.RunPython(_backfill_variant_policies, migrations.RunPython.noop),
    ]
