# Generated by Django 4.2.24 on 2025-09-10 19:15
from json import dumps, loads

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


def migrate_tool_usage_questions_forward(apps, schema_editor):
    Tool = apps.get_model("NEMO", "Tool")
    ToolUsageQuestions = apps.get_model("NEMO", "ToolUsageQuestions")
    for tool in Tool.objects.all():
        if tool._pre_usage_questions:
            ToolUsageQuestions.objects.create(
                tool=tool,
                questions=tool._pre_usage_questions,
                questions_type="pre",
            )
        if tool._post_usage_questions:
            ToolUsageQuestions.objects.create(
                tool=tool,
                questions=tool._post_usage_questions,
                questions_type="post",
            )


def migrate_tool_usage_questions_reverse(apps, schema_editor):
    Tool = apps.get_model("NEMO", "tool")
    ToolUsageQuestions = apps.get_model("NEMO", "ToolUsageQuestions")
    for tool in Tool.objects.all():
        pre_usage_questions = ToolUsageQuestions.objects.filter(tool=tool, questions_type="pre")
        post_usage_questions = ToolUsageQuestions.objects.filter(tool=tool, questions_type="post")
        if pre_usage_questions or post_usage_questions:
            if pre_usage_questions:
                pre_usage_questions_json = []
                for pre_question in pre_usage_questions:
                    pre_usage_questions_json.extend(loads(pre_question.questions))
                tool._pre_usage_questions = dumps(pre_usage_questions_json)
            if post_usage_questions:
                post_usage_questions_json = []
                for post_question in post_usage_questions_json:
                    post_usage_questions_json.extend(loads(post_question.questions))
                tool._post_usage_questions = dumps(post_usage_questions_json)
            tool.save()


class Migration(migrations.Migration):

    dependencies = [
        ("NEMO", "0124_userpreferences_login_redirect_url"),
    ]

    operations = [
        migrations.AlterField(
            model_name="reservationquestions",
            name="name",
            field=models.CharField(help_text="The name of these reservation questions", max_length=100),
        ),
        migrations.CreateModel(
            name="ToolUsageQuestions",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("enabled", models.BooleanField(default=True)),
                (
                    "display_order",
                    models.PositiveIntegerField(
                        default=1,
                        help_text="The order in which these questions will be displayed. Can be any positive integer including 0. Lower values are displayed first.",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True,
                        help_text="The optional name for these tool usage questions",
                        max_length=100,
                        null=True,
                    ),
                ),
                (
                    "questions_type",
                    models.CharField(
                        choices=[("pre", "Pre"), ("post", "Post")],
                        help_text="Should this question be asked before or after tool usage?",
                        max_length=10,
                    ),
                ),
                ("questions", models.TextField(help_text="This field will only accept JSON format")),
                (
                    "only_for_projects",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Select the projects these questions only apply to. Leave blank for all projects",
                        to="NEMO.project",
                    ),
                ),
                (
                    "tool",
                    models.ForeignKey(
                        limit_choices_to={"parent_tool__isnull": True},
                        on_delete=django.db.models.deletion.CASCADE,
                        to="NEMO.tool",
                    ),
                ),
            ],
            options={
                "ordering": ["tool", "questions_type", "display_order"],
                "verbose_name_plural": "Tool usage questions",
            },
        ),
        migrations.RunPython(migrate_tool_usage_questions_forward, migrate_tool_usage_questions_reverse),
        migrations.RemoveField(
            model_name="tool",
            name="_post_usage_questions",
        ),
        migrations.RemoveField(
            model_name="tool",
            name="_pre_usage_questions",
        ),
        migrations.AlterField(
            model_name="tool",
            name="parent_tool",
            field=models.ForeignKey(
                blank=True,
                help_text="Select a parent tool to allow alternate usage",
                limit_choices_to={"parent_tool__isnull": True},
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="tool_children_set",
                to="NEMO.tool",
            ),
        ),
        migrations.AlterField(
            model_name="toolusagecounter",
            name="tool",
            field=models.ForeignKey(
                help_text="The tool this counter is for.",
                limit_choices_to={"parent_tool__isnull": True},
                on_delete=django.db.models.deletion.CASCADE,
                to="NEMO.tool",
            ),
        ),
    ]
