# Generated by Django 3.2.15 on 2022-09-22 17:55

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


def migrate_topic_material(apps, schema_editor):
    """Migrate the activity material to activity material relation"""
    TopicMaterial = apps.get_model("topics", "TopicMaterial")
    TopicMaterialRelation = apps.get_model("topics", "TopicMaterialRelation")
    for material in list(TopicMaterial.objects.all()):
        TopicMaterialRelation.objects.create(
            material=material.material_ptr,
            topic=material.topic,
            members_only=material.members_only,
        )
        material.delete(keep_parents=True)


def undo_migrate_topic_material(apps, schema_editor):
    """Migrate the activity material relation to activity material"""
    TopicMaterial = apps.get_model("topics", "TopicMaterial")
    TopicMaterialRelation = apps.get_model("topics", "TopicMaterialRelation")
    for materialrelation in list(TopicMaterialRelation.objects.all()):
        material = materialrelation.material
        kws = model_to_dict(materialrelation)
        kws.pop("material")
        kws.update(model_to_dict(material))
        kws["user"] = material.user
        kws["category"] = material.category
        kws["license"] = material.license
        kws["date_created"] = material.date_created
        TopicMaterial.objects.create(material_ptr=material, **kws)


class Migration(migrations.Migration):

    dependencies = [
        ("activities", "0014_auto_20220919_1955"),
        ("uploaded_material", "0009_alter_material_uuid"),
        ("topics", "0010_topicrelation"),
    ]

    operations = [
        migrations.CreateModel(
            name="TopicMaterialRelation",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "members_only",
                    models.BooleanField(
                        default=False,
                        help_text="Make this material available for topic members only?",
                    ),
                ),
                (
                    "material",
                    models.ForeignKey(
                        help_text="The material that this relation corresponds to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="uploaded_material.material",
                    ),
                ),
                (
                    "topic",
                    models.ForeignKey(
                        help_text="The topic that this material belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="topics.topic",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.RunPython(
            migrate_topic_material, undo_migrate_topic_material
        ),
        migrations.DeleteModel(
            name="TopicMaterial",
        ),
    ]
