# Generated by Django 3.2.15 on 2022-09-22 18:28

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


def migrate_contribution_material(apps, schema_editor):
    """Migrate the contribution material to contribution material relation"""
    ContributionMaterial = apps.get_model("programme", "ContributionMaterial")
    ContributionMaterialRelation = apps.get_model(
        "programme", "ContributionMaterialRelation"
    )
    Material = apps.get_model("uploaded_material", "Material")
    for material in list(ContributionMaterial.objects.all()):
        ContributionMaterialRelation.objects.create(
            material=Material.objects.get(pk=material.pk),
            contribution=material.contribution,
            session=material.session,
            registered_only=material.registered_only,
            pinned=material.pinned,
        )
        material.delete(keep_parents=True)


def undo_migrate_contribution_material(apps, schema_editor):
    """Migrate the contribution material relation to contribution material"""
    ContributionMaterial = apps.get_model("programme", "ContributionMaterial")
    ContributionMaterialRelation = apps.get_model(
        "programme", "ContributionMaterialRelation"
    )
    for materialrelation in list(ContributionMaterialRelation.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
        ContributionMaterial.objects.create(
            sessionmaterial_ptr=material.sessionmaterial, **kws
        )


class Migration(migrations.Migration):

    dependencies = [
        ("uploaded_material", "0009_alter_material_uuid"),
        ("programme", "0021_auto_20220620_0720"),
    ]

    operations = [
        migrations.CreateModel(
            name="SessionMaterialRelation",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "registered_only",
                    models.BooleanField(
                        default=False,
                        help_text="Shall we make this material only available for users that registered for the event?",
                        verbose_name="Restrict access to event participants",
                    ),
                ),
                (
                    "pinned",
                    models.BooleanField(
                        default=False,
                        help_text="Should this material be shown to the session sidebar?",
                    ),
                ),
                (
                    "material",
                    models.ForeignKey(
                        help_text="The material that this relation corresponds to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="uploaded_material.material",
                    ),
                ),
                (
                    "session",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="programme.session",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="ContributionMaterialRelation",
            fields=[
                (
                    "sessionmaterialrelation_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="programme.sessionmaterialrelation",
                    ),
                ),
                (
                    "contribution",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="programme.contribution",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=("programme.sessionmaterialrelation",),
        ),
        migrations.RunPython(
            migrate_contribution_material, undo_migrate_contribution_material
        ),
        migrations.DeleteModel(
            name="ContributionMaterial",
        ),
    ]
