# Generated by Django 3.2.22 on 2026-03-31 09:56

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

# Third party
import ckeditor_uploader.fields


def copy_denormalized_data(apps, schema_editor):
    WeightedQuestion = apps.get_model("django_easy_quiz", "WeightedAnswersQuizQuestion")
    WeightedAnswer = apps.get_model("django_easy_quiz", "WeightedAnswersQuizAnswer")
    InterpretationQuestion = apps.get_model(
        "django_easy_quiz", "InterpretationQuizQuestion"
    )
    InterpretationAnswer = apps.get_model(
        "django_easy_quiz", "InterpretationQuizAnswer"
    )
    InterpretationQuiz = apps.get_model("django_easy_quiz", "InterpretationQuiz")
    InterpretationConclusion = apps.get_model(
        "django_easy_quiz", "InterpretationQuizConclusion"
    )

    weighted_questions = WeightedQuestion.objects.order_by("quiz_id", "id")
    current_quiz_id = None
    quiz_order = 0
    for question in weighted_questions:
        if question.quiz_id != current_quiz_id:
            current_quiz_id = question.quiz_id
            quiz_order = 1
        question.order = quiz_order
        question.save(update_fields=["order"])
        quiz_order += 1

        for index in range(1, 11):
            label = getattr(question, f"answer_{index}", None)
            points = getattr(question, f"points_answer_{index}", None)
            if label in (None, "") or points is None:
                continue
            WeightedAnswer.objects.create(
                question_id=question.id,
                label=label,
                points=points,
                order=index,
            )

    interpretation_questions = InterpretationQuestion.objects.order_by("quiz_id", "id")
    current_quiz_id = None
    quiz_order = 0
    for question in interpretation_questions:
        if question.quiz_id != current_quiz_id:
            current_quiz_id = question.quiz_id
            quiz_order = 1
        question.order = quiz_order
        question.save(update_fields=["order"])
        quiz_order += 1

        for index, symbol in enumerate(("■", "▲", "◆", "●"), start=1):
            label = getattr(question, f"answer_{index}", None)
            if label in (None, ""):
                continue
            InterpretationAnswer.objects.create(
                question_id=question.id,
                symbol=symbol,
                label=label,
                order=index,
            )

    for quiz in InterpretationQuiz.objects.all():
        for index, symbol in enumerate(("■", "▲", "◆", "●"), start=1):
            description = getattr(quiz, f"conclusion_max_{index}", None)
            if description in (None, ""):
                continue
            InterpretationConclusion.objects.create(
                quiz_id=quiz.id,
                symbol=symbol,
                description=description,
            )


class Migration(migrations.Migration):
    dependencies = [
        ("django_easy_quiz", "0010_weightedanswers_question_explanation"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="interpretationquizquestion",
            options={
                "ordering": ("order", "id"),
                "verbose_name": "Question de quiz d'interprétation",
                "verbose_name_plural": "Questions de quiz d'interprétation",
            },
        ),
        migrations.AlterModelOptions(
            name="weightedanswersquizquestion",
            options={
                "ordering": ("order", "id"),
                "verbose_name": "Question de quiz avec réponses pondérées",
                "verbose_name_plural": "Questions de quiz avec réponses pondérées",
            },
        ),
        migrations.AddField(
            model_name="interpretationquizquestion",
            name="order",
            field=models.PositiveIntegerField(default=0, verbose_name="Display order"),
        ),
        migrations.AddField(
            model_name="weightedanswersquizquestion",
            name="order",
            field=models.PositiveIntegerField(default=0, verbose_name="Display order"),
        ),
        migrations.AlterField(
            model_name="moreinfoquestion",
            name="is_required",
            field=models.BooleanField(
                default=False, help_text="Est-ce que la réponse est requise ?"
            ),
        ),
        migrations.AlterField(
            model_name="weightedanswersquizquestion",
            name="multiple_answers",
            field=models.BooleanField(
                default=False, verbose_name="Plusieurs réponses autorisées"
            ),
        ),
        migrations.CreateModel(
            name="WeightedAnswersQuizAnswer",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "label",
                    ckeditor_uploader.fields.RichTextUploadingField(
                        verbose_name="Answer"
                    ),
                ),
                ("points", models.IntegerField(verbose_name="Points for answer")),
                (
                    "order",
                    models.PositiveIntegerField(
                        default=0, verbose_name="Display order"
                    ),
                ),
                (
                    "question",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="answers",
                        to="django_easy_quiz.weightedanswersquizquestion",
                    ),
                ),
            ],
            options={
                "verbose_name": "Weighted Answers Quiz Answer",
                "verbose_name_plural": "Weighted Answers Quiz Answers",
                "ordering": ("order", "id"),
            },
        ),
        migrations.CreateModel(
            name="InterpretationQuizConclusion",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", ckeditor_uploader.fields.RichTextUploadingField()),
                (
                    "symbol",
                    models.CharField(
                        choices=[("■", "■"), ("▲", "▲"), ("◆", "◆"), ("●", "●")],
                        max_length=1,
                        verbose_name="Conclusion symbol",
                    ),
                ),
                (
                    "quiz",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="conclusions",
                        to="django_easy_quiz.interpretationquiz",
                    ),
                ),
            ],
            options={
                "verbose_name": "Interpretation Quiz Conclusion",
                "verbose_name_plural": "Interpretation Quiz Conclusions",
            },
        ),
        migrations.CreateModel(
            name="InterpretationQuizAnswer",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "symbol",
                    models.CharField(
                        choices=[("■", "■"), ("▲", "▲"), ("◆", "◆"), ("●", "●")],
                        max_length=1,
                        verbose_name="Answer symbol",
                    ),
                ),
                (
                    "label",
                    ckeditor_uploader.fields.RichTextUploadingField(
                        verbose_name="Answer"
                    ),
                ),
                (
                    "order",
                    models.PositiveIntegerField(
                        default=0, verbose_name="Display order"
                    ),
                ),
                (
                    "question",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="answers",
                        to="django_easy_quiz.interpretationquizquestion",
                    ),
                ),
            ],
            options={
                "verbose_name": "Interpretation Quiz Answer",
                "verbose_name_plural": "Interpretation Quiz Answers",
                "ordering": ("order", "id"),
            },
        ),
        migrations.RunPython(copy_denormalized_data, migrations.RunPython.noop),
        migrations.AddConstraint(
            model_name="interpretationquizconclusion",
            constraint=models.UniqueConstraint(
                fields=("quiz", "symbol"),
                name="uniq_interpretation_conclusion_symbol_per_quiz",
            ),
        ),
    ]
