# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-01-28 19:49
from __future__ import unicode_literals

from django.db import migrations


def migrate_kaehmys(apps, schema_editor):
    WebappTelegramChannel = apps.get_model('webapp', 'TelegramChannel')
    KaehmyTelegramChannel = apps.get_model('kaehmy', 'TelegramChannel')
    for channel in WebappTelegramChannel.objects.all():
        KaehmyTelegramChannel.objects.create(
            id=channel.id,
            name=channel.name,
            channel_id=channel.channel_id
        )

    WebappCustomKaehmyRole = apps.get_model('webapp', 'CustomKaehmyRole')
    KaehmyCustomRole = apps.get_model('kaehmy', 'CustomRole')
    for role in WebappCustomKaehmyRole.objects.all():
        KaehmyCustomRole.objects.create(
            id=role.id,
            name=role.name,
            is_board=role.is_board,
            category=role.category,
            name_fi=role.name_fi,
            name_en=role.name_en,
        )

    WebappPresetKaehmyRole = apps.get_model('webapp', 'PresetKaehmyRole')
    KaehmyPresetRole = apps.get_model('kaehmy', 'PresetRole')
    for role in WebappPresetKaehmyRole.objects.all():
        KaehmyPresetRole.objects.create(
            id=role.id,
            name=role.name,
            is_board=role.is_board,
            category=role.category,
            description=role.description,
            name_fi=role.name_fi,
            name_en=role.name_en,
            description_fi=role.description_fi,
            description_en=role.description_en,
        )

    WebappKaehmyForm = apps.get_model('webapp', 'KaehmyForm')
    KaehmyApplication = apps.get_model('kaehmy', 'Application')
    for form in WebappKaehmyForm.objects.all():
        application = KaehmyApplication.objects.create(
            id=form.id,
            name=form.name,
            email=form.email,
            timestamp=form.timestamp,
            phone_number=form.phone_number,
            year=form.year,
            text=form.text,
            custom_role_name=form.custom_role_name,
            custom_role_is_board=form.custom_role_is_board,
        )

        for custom_role in form.custom_roles.all():
            role = KaehmyCustomRole.objects.get(id=custom_role.id)
            application.custom_roles.add(role)

        for preset_role in form.preset_roles.all():
            role = KaehmyPresetRole.objects.get(id=preset_role.id)
            application.preset_roles.add(role)

        application.save()

    WebappKaehmyMessage = apps.get_model('webapp', 'KaehmyMessage')
    KaehmyComment = apps.get_model('kaehmy', 'Comment')
    KaehmyCommentParent = apps.get_model('kaehmy', 'CommentParent')
    for message in WebappKaehmyMessage.objects.order_by('id'):
        parent = KaehmyCommentParent.objects.get(id=message.parent.id)

        comment = KaehmyComment.objects.create(
            id=message.id,
            name=message.name,
            email=message.email,
            timestamp=message.timestamp,
            message=message.message,
            parent=parent,
        )


class Migration(migrations.Migration):

    dependencies = [
        ('webapp', '0037_auto_20180125_2131'),
        ('kaehmy', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(migrate_kaehmys),
    ]
