# Generated by Django 4.2.25 on 2025-11-06 13:33

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


def migrate_tool_usage_questions_forward(apps, schema_editor):
    ToolUsageQuestions = apps.get_model("NEMO", "ToolUsageQuestions")
    for tool_question in ToolUsageQuestions.objects.all():
        tool_question.name = f"{tool_question.tool.name} #{tool_question.display_order}"
        tool_question.only_for_tools.set([tool_question.tool])
        tool_question.save()


def migrate_tool_usage_questions_reverse(apps, schema_editor):
    ToolUsageQuestions = apps.get_model("NEMO", "ToolUsageQuestions")
    for tool_question in ToolUsageQuestions.objects.all():
        for tool in tool_question.only_for_tools.all():
            new_tool_usage_question = ToolUsageQuestions()
            new_tool_usage_question.name = tool_question.name
            new_tool_usage_question.enabled = tool_question.enabled
            new_tool_usage_question.display_order = tool_question.display_order
            new_tool_usage_question.questions_type = tool_question.questions_type
            new_tool_usage_question.questions = tool_question.questions
            new_tool_usage_question.tool = tool
            new_tool_usage_question.save()
            new_tool_usage_question.only_for_projects.set(tool_question.only_for_projects.all())
        tool_question.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("NEMO", "0131_user_managed_users"),
    ]

    operations = [
        migrations.AddField(
            model_name="toolusagequestions",
            name="only_for_users",
            field=models.ManyToManyField(
                blank=True,
                help_text="Select the users these questions only apply to. Leave blank for all users",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AlterModelOptions(
            name="toolusagequestions",
            options={"ordering": ["questions_type", "display_order"], "verbose_name_plural": "Tool usage questions"},
        ),
        migrations.AddField(
            model_name="toolusagequestions",
            name="only_for_tools",
            field=models.ManyToManyField(
                blank=True,
                limit_choices_to={"parent_tool__isnull": True},
                to="NEMO.tool",
                help_text="Select the tools these questions only apply to. Leave blank for all tools",
            ),
        ),
        migrations.AlterField(
            model_name="toolusagequestions",
            name="tool",
            field=models.ForeignKey(
                limit_choices_to={"parent_tool__isnull": True},
                on_delete=django.db.models.deletion.CASCADE,
                null=True,
                to="NEMO.tool",
            ),
        ),
        migrations.RunPython(migrate_tool_usage_questions_forward, migrate_tool_usage_questions_reverse),
        migrations.RemoveField(
            model_name="toolusagequestions",
            name="tool",
        ),
        migrations.AlterField(
            model_name="toolusagequestions",
            name="name",
            field=models.CharField(help_text="The name for these tool usage questions", max_length=255),
        ),
    ]
