# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-10-23 18:45
from __future__ import unicode_literals

from django.db import migrations


def move_person_from_popolo(apps, schema_editor):
    PopoloPerson = apps.get_model("popolo", "Person")
    PeoplePerson = apps.get_model("people", "Person")

    for pperson in PopoloPerson.objects.all():
        people_person = PeoplePerson.objects.create(
            pk=pperson.pk,
            start_date=pperson.start_date,
            end_date=pperson.end_date,
            created_at=pperson.created_at,
            updated_at=pperson.updated_at,
            name=pperson.name,
            family_name=pperson.family_name,
            given_name=pperson.given_name,
            additional_name=pperson.additional_name,
            honorific_prefix=pperson.honorific_prefix,
            honorific_suffix=pperson.honorific_suffix,
            patronymic_name=pperson.patronymic_name,
            sort_name=pperson.sort_name,
            email=pperson.email,
            gender=pperson.gender,
            birth_date=pperson.birth_date,
            death_date=pperson.death_date,
            summary=pperson.summary,
            biography=pperson.biography,
            national_identity=pperson.national_identity,
            versions=pperson.versions,
        )
        for election in pperson.not_standing.all():
            people_person.not_standing.add(election)


class Migration(migrations.Migration):

    dependencies = [("people", "0003_add_person_model")]

    operations = [
        migrations.RunPython(
            move_person_from_popolo, migrations.RunPython.noop
        ),
        migrations.RunSQL(
            """
               SELECT setval('people_person_id_seq', COALESCE((SELECT MAX(id)+1
               FROM people_person), 1));
            """,
            migrations.RunSQL.noop,
        ),
    ]
