# Generated by Django 5.2.10 on 2026-01-25 18:38

import django.db.models.deletion
import parler.models
from django.conf import settings
from django.db import migrations, models
from django.db.migrations.operations.base import Operation


class AlterModelBases(Operation):
    reversible = True
    reduces_to_sql = False

    def __init__(self, name, bases):
        self.name = name
        self.bases = bases

    def state_forwards(self, app_label, state):
        model_key = (app_label, self.name.lower())
        model_state = state.models[model_key]
        model_state.bases = self.bases
        state.reload_model(app_label, self.name.lower(), delay=True)

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        pass

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        pass

    def describe(self):
        return f"Alter {self.name} bases"

    def deconstruct(self):
        return (
            f"{self.__class__.__module__}.{self.__class__.__name__}",
            [],
            {"name": self.name, "bases": self.bases},
        )


def backfill_translations(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    default_language = getattr(
        settings,
        "PARLER_DEFAULT_LANGUAGE_CODE",
        getattr(settings, "LANGUAGE_CODE", "en"),
    )
    ContentClassifier = apps.get_model("content", "ContentClassifier")
    ContentClassifierTranslation = apps.get_model(
        "content", "ContentClassifierTranslation"
    )
    for classifier in ContentClassifier.objects.using(db_alias).all():
        ContentClassifierTranslation.objects.using(db_alias).get_or_create(
            master_id=classifier.id,
            language_code=default_language,
            defaults={"label": classifier.label},
        )

    ContentTag = apps.get_model("content", "ContentTag")
    ContentTagTranslation = apps.get_model("content", "ContentTagTranslation")
    for tag in ContentTag.objects.using(db_alias).all():
        ContentTagTranslation.objects.using(db_alias).get_or_create(
            master_id=tag.id,
            language_code=default_language,
            defaults={"label": tag.label},
        )

    WebRequestSampler = apps.get_model("content", "WebRequestSampler")
    WebRequestSamplerTranslation = apps.get_model(
        "content", "WebRequestSamplerTranslation"
    )
    for sampler in WebRequestSampler.objects.using(db_alias).all():
        WebRequestSamplerTranslation.objects.using(db_alias).get_or_create(
            master_id=sampler.id,
            language_code=default_language,
            defaults={
                "label": sampler.label,
                "description": sampler.description,
            },
        )

    WebRequestStep = apps.get_model("content", "WebRequestStep")
    WebRequestStepTranslation = apps.get_model("content", "WebRequestStepTranslation")
    for step in WebRequestStep.objects.using(db_alias).all():
        WebRequestStepTranslation.objects.using(db_alias).get_or_create(
            master_id=step.id,
            language_code=default_language,
            defaults={"name": step.name},
        )


class Migration(migrations.Migration):

    dependencies = [
        (
            "content",
            "0006_remove_webrequestsampler_content_webrequestsampler_owner_exclusive_and_more",
        ),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="contentclassifier",
            options={
                "ordering": ["translations__label"],
                "verbose_name": "Content Classifier",
                "verbose_name_plural": "Content Classifiers",
            },
        ),
        AlterModelBases(
            name="contentclassifier",
            bases=(parler.models.TranslatableModel, models.Model),
        ),
        migrations.AlterModelOptions(
            name="contenttag",
            options={
                "ordering": ["translations__label"],
                "verbose_name": "Content Tag",
                "verbose_name_plural": "Content Tags",
            },
        ),
        AlterModelBases(
            name="contenttag",
            bases=(parler.models.TranslatableModel, models.Model),
        ),
        migrations.AlterModelOptions(
            name="webrequestsampler",
            options={
                "ordering": ["translations__label"],
                "verbose_name": "Web Request Sampler",
                "verbose_name_plural": "Web Request Samplers",
            },
        ),
        AlterModelBases(
            name="webrequestsampler",
            bases=(parler.models.TranslatableModel, models.Model),
        ),
        AlterModelBases(
            name="webrequeststep",
            bases=(parler.models.TranslatableModel, models.Model),
        ),
        migrations.CreateModel(
            name="ContentClassifierTranslation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "language_code",
                    models.CharField(
                        db_index=True, max_length=15, verbose_name="Language"
                    ),
                ),
                ("label", models.CharField(max_length=150)),
                (
                    "master",
                    models.ForeignKey(
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="translations",
                        to="content.contentclassifier",
                    ),
                ),
            ],
            options={
                "verbose_name": "Content Classifier Translation",
                "db_table": "content_contentclassifier_translation",
                "db_tablespace": "",
                "managed": True,
                "default_permissions": (),
                "unique_together": {("language_code", "master")},
            },
            bases=(parler.models.TranslatedFieldsModelMixin, models.Model),
        ),
        migrations.CreateModel(
            name="ContentTagTranslation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "language_code",
                    models.CharField(
                        db_index=True, max_length=15, verbose_name="Language"
                    ),
                ),
                ("label", models.CharField(max_length=150)),
                (
                    "master",
                    models.ForeignKey(
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="translations",
                        to="content.contenttag",
                    ),
                ),
            ],
            options={
                "verbose_name": "Content Tag Translation",
                "db_table": "content_contenttag_translation",
                "db_tablespace": "",
                "managed": True,
                "default_permissions": (),
                "unique_together": {("language_code", "master")},
            },
            bases=(parler.models.TranslatedFieldsModelMixin, models.Model),
        ),
        migrations.CreateModel(
            name="WebRequestSamplerTranslation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "language_code",
                    models.CharField(
                        db_index=True, max_length=15, verbose_name="Language"
                    ),
                ),
                ("label", models.CharField(max_length=150)),
                ("description", models.TextField(blank=True)),
                (
                    "master",
                    models.ForeignKey(
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="translations",
                        to="content.webrequestsampler",
                    ),
                ),
            ],
            options={
                "verbose_name": "Web Request Sampler Translation",
                "db_table": "content_webrequestsampler_translation",
                "db_tablespace": "",
                "managed": True,
                "default_permissions": (),
                "unique_together": {("language_code", "master")},
            },
            bases=(parler.models.TranslatedFieldsModelMixin, models.Model),
        ),
        migrations.CreateModel(
            name="WebRequestStepTranslation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "language_code",
                    models.CharField(
                        db_index=True, max_length=15, verbose_name="Language"
                    ),
                ),
                ("name", models.CharField(blank=True, max_length=150)),
                (
                    "master",
                    models.ForeignKey(
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="translations",
                        to="content.webrequeststep",
                    ),
                ),
            ],
            options={
                "verbose_name": "Web Request Step Translation",
                "db_table": "content_webrequeststep_translation",
                "db_tablespace": "",
                "managed": True,
                "default_permissions": (),
                "unique_together": {("language_code", "master")},
            },
            bases=(parler.models.TranslatedFieldsModelMixin, models.Model),
        ),
        migrations.RunPython(backfill_translations, migrations.RunPython.noop),
        migrations.RemoveField(
            model_name="contentclassifier",
            name="label",
        ),
        migrations.RemoveField(
            model_name="contenttag",
            name="label",
        ),
        migrations.RemoveField(
            model_name="webrequestsampler",
            name="description",
        ),
        migrations.RemoveField(
            model_name="webrequestsampler",
            name="label",
        ),
        migrations.RemoveField(
            model_name="webrequeststep",
            name="name",
        ),
    ]
