# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('brainstormers', '0023_brainstormer_anonymous'),
    ]

    operations = [

        migrations.AddField(
            model_name='brainstormerproposition',
            name='brainstormer',
            field=models.ForeignKey(blank=True, to='brainstormers.Brainstormer', null=True),
        ),

        # fill the foreignkey with brainstormer.id
        migrations.RunSQL(
            """
            UPDATE brainstormers_brainstormerproposition
            SET brainstormer_id=brainstormers_brainstormerpropositiongroup.brainstormer_id
            FROM brainstormers_brainstormerpropositiongroup
            WHERE
            brainstormers_brainstormerproposition.group_id =
            brainstormers_brainstormerpropositiongroup.id
            """, reverse_sql=migrations.RunSQL.noop
        ),
        # remove the null=True
        migrations.AlterField(
            model_name='brainstormerproposition',
            name='brainstormer',
            field=models.ForeignKey(related_name='propositions',
                                    to='brainstormers.Brainstormer'),
            preserve_default=False,
        ),

        migrations.AddField(
            model_name='brainstormerproposition',
            name='groups',
            field=models.ManyToManyField(to='brainstormers.BrainstormerPropositionGroup'),
        ),

        migrations.RunSQL(
            sql="""
            INSERT INTO brainstormers_brainstormerproposition_groups
            (brainstormerproposition_id, brainstormerpropositiongroup_id)
            SELECT brainstormers_brainstormerproposition.id, group_id
            FROM brainstormers_brainstormerproposition
            JOIN brainstormers_brainstormerpropositiongroup
            ON brainstormers_brainstormerpropositiongroup.id=
                 brainstormers_brainstormerproposition.group_id
            WHERE brainstormers_brainstormerpropositiongroup.name <> 'default_group'
            """, reverse_sql=migrations.RunSQL.noop),

        migrations.RemoveField(
            model_name='brainstormerproposition',
            name='group'
        ),

        migrations.AlterField(
            model_name='brainstormerproposition',
            name='groups',
            field=models.ManyToManyField(
                to='brainstormers.BrainstormerPropositionGroup',
                related_name='propositions'),
        ),

        migrations.AddField(
            model_name='brainstormer',
            name='brainstormer_type',
            field=models.CharField(
                max_length=20,
                choices=[
                    ('p', 'propositionwall'),
                    ('q', 'questionwall'),
                    ('w', 'wordcloud')],
                default='p'),
        ),

        migrations.AddField(
            model_name='brainstormer',
            name='description',
            field=models.TextField(null=True, blank=True),
        ),

        migrations.CreateModel(
            name='PropositionWall',
            fields=[
            ],
            options={
                'proxy': True,
                'ordering': ['sort_order'],
            },
            bases=('brainstormers.brainstormer',),
        ),

        migrations.CreateModel(
            name='QuestionWall',
            fields=[
            ],
            options={
                'proxy': True,
                'ordering': ['sort_order'],
            },
            bases=('brainstormers.brainstormer',),
        ),

        migrations.CreateModel(
            name='WordCloud',
            fields=[
            ],
            options={
                'proxy': True,
                'ordering': ['sort_order'],
            },
            bases=('brainstormers.brainstormer',),
        ),
    ]
