# Generated by Django 2.2.14 on 2020-09-01 10:25

from django.db import migrations


def create_initial_owners(apps, schema_editor):
    Owner = apps.get_model("traffic_control", "Owner")
    initial_owners = (
        ("City of Helsinki", "<NAME>"),
        ("State", "Valtio"),
        ("Private", "Yksityinen"),
        ("Unknown", "Ei tiedossa"),
    )

    for name_en, name_fi in initial_owners:
        Owner.objects.get_or_create(name_en=name_en, name_fi=name_fi)


def _set_owner_relations_for_model(Owner, Model):
    """
    Naively set owner relation for all instances of given Model
    based on the existing owner string value.
    """
    helsinki = Owner.objects.get(name_en="City of Helsinki")
    state = Owner.objects.get(name_en="State")
    private = Owner.objects.get(name_en="Private")
    unknown = Owner.objects.get(name_en="Unknown")
    post_migrate_owner_str = "__migrated_to_use_owner_relation__"

    Model.objects.filter(owner__icontains="helsin").update(
        owner_obj=helsinki, owner=post_migrate_owner_str
    )
    Model.objects.filter(owner__icontains="valtio").update(
        owner_obj=state, owner=post_migrate_owner_str
    )
    Model.objects.filter(owner__icontains="yksityi").update(
        owner_obj=private, owner=post_migrate_owner_str
    )

    for instance in Model.objects.filter(owner_obj=None).exclude(owner=""):
        owner, _created = Owner.objects.get_or_create(
            name_fi=instance.owner, name_en="translation missing"
        )
        instance.owner_obj = owner
        instance.owner = post_migrate_owner_str
        instance.save(update_fields=["owner_obj", "owner"])

    Model.objects.filter(owner_obj=None).update(owner_obj=unknown)


def set_owner_relations(apps, schema_editor):
    Owner = apps.get_model("traffic_control", "Owner")
    models = (
        "AdditionalSignPlan",
        "AdditionalSignReal",
        "BarrierPlan",
        "BarrierReal",
        "MountPlan",
        "MountReal",
        "RoadMarkingPlan",
        "RoadMarkingReal",
        "SignpostPlan",
        "SignpostReal",
        "TrafficLightPlan",
        "TrafficLightReal",
        "TrafficSignPlan",
        "TrafficSignReal",
    )

    for model in models:
        Model = apps.get_model("traffic_control", model)
        _set_owner_relations_for_model(Owner, Model)


def set_owner_strings(apps, schema_editor):
    """
    Set owner string value to owner instance finnish name for
    all instances of all models that have a relation to the
    Owner model.
    """
    Owner = apps.get_model("traffic_control", "Owner")
    models = (
        "AdditionalSignPlan",
        "AdditionalSignReal",
        "BarrierPlan",
        "BarrierReal",
        "MountPlan",
        "MountReal",
        "RoadMarkingPlan",
        "RoadMarkingReal",
        "SignpostPlan",
        "SignpostReal",
        "TrafficLightPlan",
        "TrafficLightReal",
        "TrafficSignPlan",
        "TrafficSignReal",
    )

    for model in models:
        Model = apps.get_model("traffic_control", model)

        for owner in Owner.objects.all():
            Model.objects.filter(owner_obj=owner).update(owner=owner.name_fi)


class Migration(migrations.Migration):

    dependencies = [
        ("traffic_control", "0021_owner_model"),
    ]

    operations = [
        migrations.RunPython(create_initial_owners, migrations.RunPython.noop),
        migrations.RunPython(set_owner_relations, set_owner_strings),
    ]
