# Generated by Django 3.2.7 on 2022-01-19 15:21


# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
#
# This file is part of django-academic-community and is released under the
# EUPL-1.2 license.
# See LICENSE in the root of the repository for full licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the EUROPEAN UNION PUBLIC LICENCE v. 1.2 or later
# as published by the European Commission.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# EUPL-1.2 license for more details.
#
# You should have received a copy of the EUPL-1.2 license along with this
# program. If not, see https://www.eupl.eu/.


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


def migrate_topics(apps, schema_editor):
    """Migrate the CLM topics to the new table."""
    Topic = apps.get_model("topics", "Topic")
    CLMTopic = apps.get_model("topics", "CLMTopic")

    for topic in Topic.objects.all():
        clmtopic = CLMTopic(
            topic_ptr_id=topic.id,
            compartment=topic.legacy_compartment,
            component=topic.legacy_component,
            desired_cooperations=topic.legacy_desired_cooperations,
            is_internal=topic.legacy_is_internal,
            max_resolution=topic.legacy_max_resolution,
            min_resolution=topic.legacy_min_resolution,
            model_availability=topic.legacy_model_availability,
            program=topic.legacy_program,
            publication=topic.legacy_publication,
            references=topic.legacy_references,
            resolution=topic.legacy_resolution,
            status=topic.legacy_status,
        )

        clmtopic.__dict__.update(
            {
                key: val
                for key, val in topic.__dict__.items()
                if not key.startswith("legacy_")
            }
        )

        clmtopic.save()

        clmtopic.usage.add(*topic.legacy_usage.all())
        clmtopic.eval_data.add(*topic.legacy_eval_data.all())
        clmtopic.forcing.add(*topic.legacy_forcing.all())
        clmtopic.lbc.add(*topic.legacy_lbc.all())
        clmtopic.simulation_model.add(*topic.legacy_simulation_model.all())


def revert_topics(apps, schema_editor):
    """Migrate the CLM topics to the new table."""
    Topic = apps.get_model("topics", "Topic")
    CLMTopic = apps.get_model("topics", "CLMTopic")

    for clmtopic in CLMTopic.objects.all():
        try:
            topic = Topic.objects.get(id=clmtopic.id)
        except Topic.DoesNotExist:
            pass
        else:

            Topic.objects.filter(id=clmtopic.id).update(
                legacy_compartment=clmtopic.compartment,
                legacy_component=clmtopic.component,
                legacy_desired_cooperations=clmtopic.desired_cooperations,
                legacy_is_internal=clmtopic.is_internal,
                legacy_max_resolution=clmtopic.max_resolution,
                legacy_min_resolution=clmtopic.min_resolution,
                legacy_model_availability=clmtopic.model_availability,
                legacy_program=clmtopic.program,
                legacy_publication=clmtopic.publication,
                legacy_references=clmtopic.references,
                legacy_resolution=clmtopic.resolution,
                legacy_status=clmtopic.status,
            )

            topic.legacy_usage.add(*clmtopic.usage.all())
            topic.legacy_region.add(*clmtopic.region.all())
            topic.legacy_eval_data.add(*clmtopic.eval_data.all())
            topic.legacy_forcing.add(*clmtopic.forcing.all())
            topic.legacy_lbc.add(*clmtopic.lbc.all())
            topic.legacy_simulation_model.add(*clmtopic.simulation_model.all())


class Migration(migrations.Migration):

    dependencies = [
        ("topics", "0005_auto_20210525_1702"),
    ]

    operations = [
        migrations.RenameField(
            model_name="topic",
            old_name="compartment",
            new_name="legacy_compartment",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="component",
            new_name="legacy_component",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="desired_cooperations",
            new_name="legacy_desired_cooperations",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="eval_data",
            new_name="legacy_eval_data",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="forcing",
            new_name="legacy_forcing",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="is_internal",
            new_name="legacy_is_internal",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="lbc",
            new_name="legacy_lbc",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="max_resolution",
            new_name="legacy_max_resolution",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="min_resolution",
            new_name="legacy_min_resolution",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="model_availability",
            new_name="legacy_model_availability",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="program",
            new_name="legacy_program",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="publication",
            new_name="legacy_publication",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="references",
            new_name="legacy_references",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="region",
            new_name="legacy_region",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="resolution",
            new_name="legacy_resolution",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="simulation_model",
            new_name="legacy_simulation_model",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="status",
            new_name="legacy_status",
        ),
        migrations.RenameField(
            model_name="topic",
            old_name="usage",
            new_name="legacy_usage",
        ),
        migrations.CreateModel(
            name="CLMTopic",
            fields=[
                (
                    "topic_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="topics.topic",
                    ),
                ),
                (
                    "desired_cooperations",
                    djangocms_text_ckeditor.fields.HTMLField(
                        blank=True,
                        help_text="More details about (desired) cooperations to other topics and communities.",
                        max_length=40000,
                        null=True,
                    ),
                ),
                (
                    "is_internal",
                    models.BooleanField(
                        default=True,
                        help_text="Is the topic for internal use only?.",
                    ),
                ),
                (
                    "resolution",
                    models.CharField(
                        blank=True,
                        help_text="Resolutions covered in the model [km].",
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "min_resolution",
                    models.FloatField(
                        blank=True,
                        help_text="Minimum resolution of the models within the topic [km].",
                        null=True,
                    ),
                ),
                (
                    "max_resolution",
                    models.FloatField(
                        blank=True,
                        help_text="Maximum resolution of the models within the topic [km].",
                        null=True,
                    ),
                ),
                (
                    "model_availability",
                    models.DateField(
                        blank=True,
                        help_text="Planned availability of the developped model.",
                        null=True,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("ongoing", "Ongoing"),
                            ("planned", "Planned"),
                            ("finished", "Finished"),
                        ],
                        help_text="The status of the topic.",
                        max_length=8,
                        null=True,
                    ),
                ),
                (
                    "references",
                    djangocms_text_ckeditor.fields.HTMLField(
                        blank=True,
                        help_text="Other references to this topic.",
                        max_length=40000,
                        null=True,
                    ),
                ),
                (
                    "publication",
                    djangocms_text_ckeditor.fields.HTMLField(
                        blank=True,
                        help_text="Peer-reviewed articles published from this topic.",
                        max_length=40000,
                        null=True,
                    ),
                ),
                (
                    "program",
                    models.CharField(
                        blank=True,
                        help_text="Name of a superior program.",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "compartment",
                    models.ForeignKey(
                        blank=True,
                        help_text="Model compartment",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="topics.modelcompartmentdevelopment",
                        verbose_name="Field of investigation",
                    ),
                ),
                (
                    "component",
                    models.ForeignKey(
                        blank=True,
                        help_text="Name of the Component under development.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="topics.couplingcomponent",
                        verbose_name="Coupling component",
                    ),
                ),
                (
                    "eval_data",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Evaluation datasets.",
                        to="topics.EvalData",
                    ),
                ),
                (
                    "forcing",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Forcing datasets used within the topic.",
                        to="topics.Forcing",
                    ),
                ),
                (
                    "lbc",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Local boundary conditions used for this topic.",
                        to="topics.LocalBoundaryCondition",
                        verbose_name="local boundary conditition",
                    ),
                ),
                (
                    "region",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Focus regions of this topic.",
                        to="topics.Region",
                    ),
                ),
                (
                    "simulation_model",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Simulation models used within this topic.",
                        to="topics.SimulationModel",
                        verbose_name="Model version",
                    ),
                ),
                (
                    "usage",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Usage of the topic.",
                        to="topics.Usage",
                    ),
                ),
            ],
            bases=("topics.topic",),
        ),
        migrations.RunPython(migrate_topics, revert_topics),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_compartment",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_component",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_desired_cooperations",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_eval_data",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_forcing",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_is_internal",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_lbc",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_max_resolution",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_min_resolution",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_model_availability",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_program",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_publication",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_references",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_region",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_resolution",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_simulation_model",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_status",
        ),
        migrations.RemoveField(
            model_name="topic",
            name="legacy_usage",
        ),
    ]
