from django.db import migrations, models


def migrate_featured_stable_to_channels(apps, schema_editor):
    Version = apps.get_model("products", "SubProductVersion")
    for version in Version.objects.filter(is_featured_stable=True):
        version.is_featured = True
        if not version.release_channel:
            version.release_channel = "stable"
        version.save(update_fields=["is_featured", "release_channel"])


class Migration(migrations.Migration):

    dependencies = [
        ("products", "0020_video_description_format"),
    ]

    operations = [
        migrations.AddField(
            model_name="subproductversion",
            name="channel_label",
            field=models.CharField(
                blank=True,
                help_text="Optional custom badge text. Overrides the preset channel label when set.",
                max_length=50,
            ),
        ),
        migrations.AddField(
            model_name="subproductversion",
            name="is_featured",
            field=models.BooleanField(
                default=False,
                help_text="Primary release shown at the top of the downloads section.",
            ),
        ),
        migrations.AddField(
            model_name="subproductversion",
            name="release_channel",
            field=models.CharField(
                blank=True,
                choices=[
                    ("", "None"),
                    ("stable", "Stable"),
                    ("beta", "Beta"),
                    ("rc", "Release candidate"),
                    ("preview", "Preview"),
                    ("nightly", "Nightly"),
                    ("current", "Current"),
                    ("previous", "Previous"),
                ],
                default="",
                help_text="Preset badge for this release (Stable, Beta, Previous, etc.).",
                max_length=20,
            ),
        ),
        migrations.AddField(
            model_name="subproductversion",
            name="show_on_product_page",
            field=models.BooleanField(
                default=False,
                help_text="Also show this release on the product page (e.g. beta or previous version).",
            ),
        ),
        migrations.RunPython(migrate_featured_stable_to_channels, migrations.RunPython.noop),
        migrations.RemoveField(
            model_name="subproductversion",
            name="is_featured_stable",
        ),
    ]
