# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2021-03-05 16:33
from __future__ import unicode_literals

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


def element_to_frozen_element(apps, schema_editor):
    Answer = apps.get_model("review", "ReviewAssignmentAnswer")
    Frozen = apps.get_model("review", "FrozenReviewFormElement")

    for answer in Answer.objects.all():
        f = Frozen(
            name=answer.element.name,
            kind=answer.element.kind,
            choices=answer.element.choices,
            required=answer.element.required,
            order=answer.element.order,
            width=answer.element.width,
            help_text=answer.element.help_text,
            default_visibility=answer.element.default_visibility,
        )
        f.answer = answer
        f.form_element = answer.element
        f.save()
        answer.original_element = answer.element
        answer.save


def frozen_element_to_element(apps, schema_editor):
    Frozen = apps.get_model("review", "FrozenReviewFormElement")
    for frozen in Frozen.objects.all():
        if frozen.form_element:
            answer = frozen.answer
            answer.element = frozen.form_element
            answer.save()
            frozen.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('review', '0010_answer_not_required'),
    ]

    operations = [
        migrations.AlterField(
            model_name='reviewassignmentanswer',
            name='element',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='review.ReviewFormElement'),
        ),
        migrations.CreateModel(
            name='FrozenReviewFormElement',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
                ('kind', models.CharField(choices=[('text', 'Text Field'), ('textarea', 'Text Area'), ('check', 'Check Box'), ('select', 'Select'), ('email', 'Email'), ('upload', 'Upload'), ('date', 'Date')], max_length=50)),
                ('choices', models.CharField(blank=True, help_text='Seperate choices with the bar | character.', max_length=1000, null=True)),
                ('required', models.BooleanField(default=True)),
                ('order', models.IntegerField()),
                ('width', models.CharField(choices=[('large-4 columns', 'third'), ('large-6 columns', 'half'), ('large-12 columns', 'full')], max_length=20)),
                ('help_text', models.TextField(blank=True, null=True)),
                ('default_visibility', models.BooleanField(default=True, help_text='If true, this setting will be available to the author automatically, if false it willbe hidden to the author by default.')),
            ],
            options={
                'ordering': ('order', 'name'),
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='frozenreviewformelement',
            name='answer',
            field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='frozen_element', to='review.ReviewAssignmentAnswer'),
        ),
        migrations.AddField(
            model_name='reviewassignmentanswer',
            name='original_element',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='review.ReviewFormElement'),
        ),
        migrations.AddField(
            model_name='frozenreviewformelement',
            name='form_element',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='review.ReviewFormElement'),
        ),
        migrations.RunPython(element_to_frozen_element, reverse_code=frozen_element_to_element),
        migrations.AlterField(
            model_name='reviewassignment',
            name='form',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='review.ReviewForm'),
        ),
        migrations.RemoveField(
            model_name='reviewassignmentanswer',
            name='element',
        ),
    ]