# Generated by Django 3.2.7 on 2022-02-04 11:39


# 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
from django.conf import settings
from django.db import migrations, models


def migrate_members(apps, schema_editor):
    """Migrate the CLM topics to the new table."""
    CommunityMember = apps.get_model("members", "CommunityMember")
    CommunityMemberWithAgreement = apps.get_model(
        "members", "CommunityMemberWithAgreement"
    )

    for member in CommunityMember.objects.all():
        new_member = CommunityMemberWithAgreement(
            communitymember_ptr_id=member.id,
            community_agreement=member.legacy_community_agreement,
        )

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

        new_member.save()


def revert_members(apps, schema_editor):
    """Migrate the CLM topics to the new table."""
    CommunityMember = apps.get_model("members", "CommunityMember")
    CommunityMemberWithAgreement = apps.get_model(
        "members", "CommunityMemberWithAgreement"
    )

    for new_member in CommunityMemberWithAgreement.objects.all():
        try:
            CommunityMember.objects.get(id=new_member.id)
        except CommunityMember.DoesNotExist:
            pass
        else:
            CommunityMember.objects.filter(id=new_member.id).update(
                legacy_community_agreement=new_member.community_agreement
            )


delete_operations = [
    migrations.RemoveField(
        model_name="communitymember",
        name="legacy_community_agreement",
    ),
    migrations.RemoveField(
        model_name="communitymemberwithagreement",
        name="community_agreement",
    ),
    migrations.RemoveField(
        model_name="communitymemberwithagreement",
        name="communitymember_ptr",
    ),
    migrations.DeleteModel(
        name="CommunityAgreement",
    ),
    migrations.DeleteModel(
        name="CommunityMemberWithAgreement",
    ),
]

rename_operations = [
    migrations.AlterModelTable(
        name="CommunityAgreement",
        table="clm_registration_communityagreement",
    ),
    migrations.AlterModelTable(
        name="CommunityMemberWithAgreement",
        table="clm_registration_communitymemberwithagreement",
    ),
]


if "clm_registration" in settings.INSTALLED_APPS:
    database_operations = rename_operations
else:
    database_operations = delete_operations


class Migration(migrations.Migration):

    dependencies = [
        ("members", "0005_auto_20210528_1324"),
        ("registrations", "0002_auto_20220111_0901"),
        ("programme", "0010_alter_meetingroom_description"),
        ("topics", "0007_auto_20220119_2102"),
        ("institutions", "0003_auto_20210424_1438"),
        ("activities", "0009_remove_activity_mailing_list"),
        ("events", "0001_initial"),
    ]

    operations = [
        migrations.RenameField(
            model_name="communitymember",
            old_name="community_agreement",
            new_name="legacy_community_agreement",
        ),
        migrations.CreateModel(
            name="CommunityMemberWithAgreement",
            fields=[
                (
                    "communitymember_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="members.communitymember",
                    ),
                ),
                (
                    "community_agreement",
                    models.ForeignKey(
                        blank=True,
                        help_text="The community agreement the member accepted",
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        to="members.communityagreement",
                    ),
                ),
            ],
            bases=("members.communitymember",),
        ),
        migrations.RunPython(migrate_members, revert_members),
        migrations.SeparateDatabaseAndState(
            state_operations=delete_operations,
            database_operations=database_operations,
        ),
    ]
